GROUP BY issue when SELECT to dataGridView from 2 tables

空扰寡人 提交于 2019-12-01 23:31:20

If I'm understanding what you're after, this is the part that's keeping you from getting the rows with no entries in klisluz:

WHERE zajsluz.akce= '{0}' and klisluz.subkey ='" + vyberradek + "'

For the rows that don't exist in klisluz, subkey will be NULL, which won't match anything with that WHERE clause. To get these rows as well, you can replace your current WHERE clause with:

WHERE zajsluz.akce= '{0}' and ISNULL(klisluz.subkey, '" + vyberradek + "') ='" + vyberradek + "'

Here's the full line of code:

string sQuery = string.Format("SELECT zajsluz.akce,zajsluz.text,klisluz.pocet,klisluz.subkey,zajsluz.ID FROM zajsluz LEFT JOIN klisluz ON zajsluz.ID=klisluz.IDzajsluz WHERE zajsluz.akce= '{0}' and ISNULL(klisluz.subkey, '" + vyberradek + "') = '" + vyberradek + "' GROUP BY klisluz.subkey,zajsluz.akce,zajsluz.text,klisluz.pocet,zajsluz.ID", sZakce);

To determine whether the checkbox should be checked:

if (precti3.HasRows)
{
    precti3.Read();
    if (precti3.Item("subkey") != Null)
    {
        row.Cells[5].Value = true;
    }
    else
    {
        row.Cells[5].Value = false;
    }
}

I think that's how it'd go in C#. I work in VB.NET, used an online converter for this.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!