-1

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

   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"]);

                }

}

4

1 回答 1

1

This is the first thing i found when I googled how to programaticly select a item in the list.

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 awnser was found on this post, and posted by wdavo.

于 2015-03-26T14:17:42.690 回答