-1

I want to match the values from reader and checkbox to change selected values of item of checkboxlist. But it does not work and I don't know what to do? Thanks.

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

I tried also,

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

                }

}

lkalay
  • 89
  • 1
  • 1
  • 10
  • 1
    Define "does not work". Does it not compile? Does it throw an exception? Does it produce the wrong result? Does nothing happen? – Oblivious Sage Mar 26 '15 at 13:13
  • the code has error. it is turkish, I translate it as" object is not set by example of an object" – lkalay Mar 26 '15 at 13:20
  • @lkalay I would imagine that is because FindByValue is returning null and then you are trying to set the Selected property on it - have you debugged? (i.e split in to 2 steps and see what FindByValue is returning) – Kevin Main Mar 26 '15 at 13:22
  • How can i search or find of item values in checkboxlist to change selected value – lkalay Mar 26 '15 at 13:26
  • @lkalay you are correct in the way you are doing it - it just appears that whatever reader["malzeme_id"] resolves to is not in the list. I would imagine it would be case sensitive - could that be the issue? Can you show what is in you list and what reader["malzeme_id"] is? – Kevin Main Mar 26 '15 at 13:31
  • Did you debug to check if the reader has values? – Kami Mar 26 '15 at 13:43

1 Answers1

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.

Community
  • 1
  • 1
maam27
  • 444
  • 3
  • 21