简体   繁体   中英

Using List content for filtering datagridview C#

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM