Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set checked items in checkedlistbox from list or dataset

I have a CheckedListBox and I would like to check all the items that are in another List. This code does not work since the CheckedItems property is read-only and the types do not match, but it gives the best idea of what I want to do.

    checkedListBox1.DataSource = DataSetSelectAll().Tables[0];
    checkedListBox1.ValueMember = "id_table";
    checkedListBox1.DisplayMember = "name";

    List<tableClass> list = MyCheckedList();
    checkedListBox1.CheckedItems = list;

I know this is wrong but do not know how to explain it better.

like image 943
Xtian11 Avatar asked Nov 03 '12 01:11

Xtian11


1 Answers

Its not possible to set(check) many items at a time like this, checkedListBox1.CheckedItems = list;

better you can use for loop like:

List<tableClass> list = MyCheckedList();
for (int count = 0; count < checkedListBox1.Items.Count; count++)
{
  if (list.Contains(checkedListBox1.Items[count].ToString()))
  {
    checkedListBox1.SetItemChecked(count, true);
  }
}
like image 170
andy Avatar answered Sep 20 '22 16:09

andy