Using List content for filtering datagridview C#

时间:2017-04-06 16:43:58

标签: c# sql-server datagridview bindingsource toolstripmenu

I've been struggling with this question for a few days now and I haven't found any answer yet; I created a ToolStripMenu array which is filled dynamically from a stored procedure:

ToolStripMenuItem[] itemsDepto = null;
itemsDepto = new ToolStripMenuItem[data.Tables[0].Rows.Count];
for (int i = 0; i <= data.Tables[0].Rows.Count - 1; i++)
{
     itemsDepto[i] = new ToolStripMenuItem();
     itemsDepto[i].Tag = data.Tables[0].Rows[i].ItemArray[0];
     itemsDepto[i].Text = data.Tables[0].Rows[i].ItemArray[1].ToString();
     itemsDepto[i].CheckOnClick = true;
     itemsDepto[i].Checked = true;
     itemsDepto[i].Click += DeptoFilter_Click;
     deptoList.Add(data.Tables[0].Rows[i].ItemArray[1].ToString());
}
tsmiDepartamento.DropDownItems.AddRange(itemsDepto);

And what I'm trying to achieve is use this ToolStripMenu as a filter control for the user, by default is Checked so when the user unchecks the menu, it should filter the rows with the content that is unchecked.

In the click event I add and remove values from the list depending on the state of the menu button as you can see in the following example:

private void DeptoFilter_Click(object sender, EventArgs e)
{
    ToolStripMenuItem temp = new ToolStripMenuItem();
    temp = (ToolStripMenuItem)sender;
    BindingSource bind = new BindingSource();
    bind.DataSource = dgvPersonalTotal.DataSource;
    if (temp.CheckState == CheckState.Checked)
    {
        deptoList.Add(sender.ToString());
    }
    else
    {
        deptoList.Remove(sender.ToString());
    }
    bind.Filter = "Departamento NOT IN (" + /*LIST*/"" + ")";
    dgvPersonalTotal.DataSource = bind;
    //foreach (string x in deptoList)
    //{
    //}
}

But the big question I have is, how can I use a list to filter the Binding Source, as you can see in the code, I can't just use the list or even try use the BindingSource.Filter in the foreach, I don't know how to resolve this problem, so any idea is well appreciated.

1 个答案:

答案 0 :(得分:0)

bind.Filter = "Departamento NOT IN (" + string.Join(",", deptoList.ToArray()) + ")";