C#checkedListBox的最后一项未显示

C#checkedListBox的最后一项未显示,c#,checkedlistbox,C#,Checkedlistbox,此代码预过滤dataGridView,以便仅显示列表中的选中项 我面临的问题是,代码每次都会以某种方式忽略最后一项,除非我取消选中,再次选中并按“Go” 这是我的代码: public partial class Notifications : Form { string filterstring = ""; int count = 0; private void checkedListBox_ItemCheck(object sender, System.EventAr

此代码预过滤dataGridView,以便仅显示列表中的选中项

我面临的问题是,代码每次都会以某种方式忽略最后一项,除非我取消选中,再次选中并按“Go”

这是我的代码:

public partial class Notifications : Form
{
    string filterstring = "";
    int count = 0;
    private void checkedListBox_ItemCheck(object sender, System.EventArgs e)
    {
        //  Loop through all items in the checkedBoxes.

        foreach (object itemChecked in checkedListBox.CheckedItems)
        {
            if (count != 0)
            {
                filterstring += "OR Responsible = '" + itemChecked.ToString() + "'";
            }
            else
                filterstring += "Responsible = '" + itemChecked.ToString() + "'";
            count += 1;
        }
    }
  private void button1_Click(object sender, EventArgs e)
    {
            DataTableCollection tables = myDatabaseDataSet.Tables;
            DataView view = new DataView(tables[0]);
            BindingSource source = new BindingSource();
            source.DataSource = view;
            dataGridView1.DataSource = source;
            source.Filter = filterstring;
    }
我知道这个解决办法可能很傻,但我想不出来

private void checkedListBox_ItemCheck(object sender, ItemCheckEventArgs e)
{
    if (e.NewValue == CheckState.Checked)
        filterstring = "Responsible = '" + checkedListBox.Items[e.Index].ToString() + "' OR";
    //  Loop through all items in the checkedBoxes.

    foreach (object itemChecked in checkedListBox.CheckedItems)
    {
         filterstring += " Responsible = '" + itemChecked.ToString() + "' OR";
    }
    filterstring = filterstring.Substring(0, filterstring.LastIndexOf("OR"));
}
您必须检查单击项的新值。如果选中,则可以将其包含在筛选字符串中。此外,删除最后一个或比在每个迭代器中删除if要好得多。 编辑:您的事件是EventArgs,但它应该是ItemCheckEventArgs

您必须检查单击项的新值。如果选中,则可以将其包含在筛选字符串中。此外,删除最后一个或比在每个迭代器中删除if要好得多。
编辑:您的事件是EventArgs,但它应该是ItemCheckEventArgs

检查状态直到ItemCheck事件发生后才更新。检查状态直到ItemCheck事件发生后才更新。