C# checkedListBox 的最后一项未显示

C# Last Item of the checkedListBox is not showing up

此代码预过滤 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"));
}

您必须检查项目的新值是否被点击。如果选中,您可以将其包含在过滤字符串中。此外,删除最后一个 OR 比在每个迭代器中删除要好得多。 编辑:您的事件是 EventArgs Bu 它应该是 ItemCheckEventArgs