سؤال

I've made customCheckedListBox, which I want to use to filter dataGridView with mulitselect option. I would like to be able to catch CheckedListBox CheckedChange state, but CheckedListBox only supports ItemCheck event.

Here is my code:

private void customCheckedListBox1_ItemCheck(object sender, ItemCheckEventArgs e)
{
   FilterDataGrid();
}

private void FilterDataGrid()
{
    var list4 = customCheckedListBox1.SelectedItems.Cast<string>().ToList();
    if (customCheckedListBox1.SelectedItems.Count != 0)
    {
       var result = list3.Where(Srodek => list4.Any(x => x == Srodek.Srodek.category1));
         DataTable ListAsDataTable3 = BuildDataTable2<CalaLinijka>(result);
         DataView ListAsDataView3 = ListAsDataTable3.DefaultView;
         dataGridView4.DataSource = view = ListAsDataView3;
    }

}

Problem is that ItemCheck event can handle only one choice, so even when user decided to choose more than one opiton it will show only first selected item. I guess that CheckedChanged event would work in my case, but when ItemCheck event is called there are no CheckedItems yet. They become "Checked" after ItemCheck event is finished. So when it goes inside FilterDataGrid CheckedChanged.Count equals to 0.

My question is how should I handle CheckedChanged event in CheckedListBox. I hope that I didn't messed up too much. If there will be any questions, just let me know and I will try to expain more.

هل كانت مفيدة؟

المحلول

I solved this problem by using foreach loop (just like KingKing suggested) and putting it inside MouseLeave event.

private void customCheckedListBox1_MouseLeave(object sender, EventArgs e)
{
    foreach (string itemChecked in customCheckedListBox1.CheckedItems)
    {
        CheckedList.Add(itemChecked);
    }
    FilterDataGrid();
}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top