简体   繁体   中英

How can I filter dataGridView by checking multiple items in CheckedListBox?

I have this code to filter my dataGridView using the checkedListBox. Every time the user checks a box in the checkedListBox, the dataGridView automatically updates to show only data related to the checked name (eg filtered by checked name = "John") and it works pretty well.

private void checkedListBox1_ItemCheck(object sender, ItemCheckEventArgs e)
    {
                DataTableCollection tables = myDatabaseDataSet.Tables;
                DataView view = new DataView(tables[0]);
                BindingSource source = new BindingSource();
                source.DataSource = view;
                dataGridView1.DataSource = source;
                source.Filter = "myColumnName ='" + checkedListBox1.Text.Replace("'", "''") + "'";
        }

Now the question is, how could I make it so multiple items in the checkedListBox are checked and in turn dataGridView updates by showing only the names checked (eg checked names in the checkedListBox are "John" and "Jane")?

The above code gives me the following result:

Code Above

What I want to achieve is this (mocked picture):

Desired outcome

Any help is appreciated.

So you will have a database that will result in some form of "Select distinct name from database" to populate a checkedListBox. So now you'll have to add something like:

List<string> names = new List<string>();
for (int i = 0; i < checkedListBox.Items.Count; i++) 
{
    CheckState st = checkedListBox.GetItemCheckState(checkedListBox.Items.IndexOf(i));
    if(st == CheckState.Checked)
    {
        int selected = checkedListBox.SelectedIndex;
        if (selected != -1)
        {
            names.Add(checkedListBox.Items[selected].ToString());
        }
    }
}  

The result of that will be a list of the items in checkedListBox that are checked. You can then use that with the code i gave previously to filter. Just replace the hardcoded names with the checked list strings.

string filterString = "";
int count = 0;
foreach (name in names)
{
    if (count != 0)
    {
        filterString += " OR Responsible = '" + name + "'";
    }
    else
    {
        filterString += "Responsible = '" + name + "'";
    }
    count += 1;
}

Now you have a string that can be used as a filter for creating the DataView:

DataView view = new DataView(tables[0], filterString, "Responsible Desc", DataViewRowState.CurrentRows);

This should filter the table when it becomes a DataView as opposed to after, based on the checkBox states.

Final event:

private void checkedListBox1_ItemCheck(object sender, ItemCheckEventArgs e)
{
    DataTableCollection tables = myDatabaseDataSet.Tables;

    //
    // Place all my code here
    //

    BindingSource source = new BindingSource();
    source.DataSource = view;
    dataGridView1.DataSource = source;
}

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