Set checked items in checkedlistbox from list or dataset

Xti*_*n11 4 c# dataset checkedlistbox winforms checklistbox

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;
Run Code Online (Sandbox Code Playgroud)

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

and*_*ndy 7

它不可能像这样设置(检查)多个项目,checkedListBox1.CheckedItems = list;

你可以更好地使用for循环:

List<tableClass> list = MyCheckedList();
for (int count = 0; count < checkedListBox1.Items.Count; count++)
{
  if (list.Contains(checkedListBox1.Items[count].ToString()))
  {
    checkedListBox1.SetItemChecked(count, true);
  }
}
Run Code Online (Sandbox Code Playgroud)