如何获得复选框列表项的值? C#

How can I get value in values of item of checkboxlist ? c#

我想匹配 reader 和复选框的值,以更改复选框列表项的选定值。但它不起作用,我不知道该怎么办?谢谢

   while (reader.Read())
        {
                       CheckBoxList1.Items.FindByValue(reader["malzeme_id"].ToString()).Selected = true;
         }

我也试过了,

while (reader.Read())
        {

for (int i = 0; i < CheckBoxList1.Items.Count; i++)
            {

                if (CheckBoxList1.Items[i].Value.Equals(reader["malzeme_id"].ToString()))
                {

                    CheckBoxList1.Items[i].Selected = Convert.ToBoolean( reader["isSelected"]);

                }

}

这是我在谷歌上搜索如何以编程方式 select 列表中的项目时发现的第一件事。

Assuming that the items in your CheckedListBox are strings:

for (int i = 0; i < checkedListBox1.Items.Count; i++)

 {
  if ((string)checkedListBox1.Items[i] == value)
   {
    checkedListBox1.SetItemChecked(i, true);
   }
}

Or

int index = checkedListBox1.Items.IndexOf(value);

if (index >= 0)
{
  checkedListBox1.SetItemChecked(index, true);
}

此答案是在 this post 上找到的,post 由 wdavo 编辑。