19

In C#, I am trying to Check an item in a CheckBoxList where the text equals what I require.

I would modify the code to check items that exist in the database.

If you would like an example, I need to select the checklistbox item that equals to abc.

peterh
  • 11,875
  • 18
  • 85
  • 108
Peter Roche
  • 335
  • 2
  • 6
  • 18
  • Have you attempted this yourself yet? Is the interface to the CheckBoxList something you dont understand? – Fuzz Feb 07 '12 at 23:44
  • Please don't prefix your titles with "C#" and such. That's what the tags are for. – John Saunders Feb 07 '12 at 23:46
  • 2
    Please see the FAQ regarding signatures in posts. I also removed your thank you line, because it's more likely than not that no one will help since you haven't shown that you've tried to help yourself. – M.Babcock Feb 07 '12 at 23:46
  • 1
    I have spent the last two hours trying to get ways of working it but i can't seem to figure how to get the checkedListBox.SetItemChecked to relate it to an item in the checklistbox. – Peter Roche Feb 07 '12 at 23:53

5 Answers5

45

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);
  }
wdavo
  • 5,070
  • 1
  • 19
  • 20
10

Example based on ASP.NET CheckBoxList

<asp:CheckBoxList ID="checkBoxList1" runat="server">
    <asp:ListItem>abc</asp:ListItem>
    <asp:ListItem>def</asp:ListItem>
</asp:CheckBoxList>


private void SelectCheckBoxList(string valueToSelect)
{
    ListItem listItem = this.checkBoxList1.Items.FindByText(valueToSelect);

    if(listItem != null) listItem.Selected = true;
}

protected void Page_Load(object sender, EventArgs e)
{
    SelectCheckBoxList("abc");
}
Jim Scott
  • 2,493
  • 1
  • 18
  • 16
5

All Credit to @Jim Scott -- just added one touch. (ASP.NET 4.5 & C#)

Refractoring this a little more... if you pass the CheckBoxList as an object to the method, you can reuse it for any CheckBoxList. Also you can use either the Text or the Value.

private void SelectCheckBoxList(string valueToSelect, CheckBoxList lst)
{
    ListItem listItem = lst.Items.FindByValue(valueToSelect);
    //ListItem listItem = lst.Items.FindByText(valueToSelect);
    if (listItem != null) listItem.Selected = true;
}

//How to call it -- in this case from a SQLDataReader and "chkRP" is my CheckBoxList`

SelectCheckBoxList(dr["kRPId"].ToString(), chkRP);`
dckuehn
  • 2,427
  • 3
  • 27
  • 37
Danimal111
  • 1,976
  • 25
  • 31
0

//Multiple selection:

          private void clbsec(CheckedListBox clb, string text)
          {
              for (int i = 0; i < clb.Items.Count; i++)
              {
                  if(text == clb.Items[i].ToString())
                  {
                      clb.SetItemChecked(i, true);
                  }
              }
          }

using ==>

clbsec(checkedListBox1,"michael");

or 

clbsec(checkedListBox1,textBox1.Text);

or

clbsec(checkedListBox1,dataGridView1.CurrentCell.Value.toString());
ardem
  • 21
  • 5
0

I tried adding dynamically created ListItem and assigning the selected value.

foreach(var item in yourListFromDB)
{
 ListItem listItem = new ListItem();
 listItem.Text = item.name;
 listItem.Value = Convert.ToString(item.value);
 listItem.Selected=item.isSelected;                 
  checkedListBox1.Items.Add(listItem);
}
checkedListBox1.DataBind();

avoid using binding the DataSource as it will not bind the checked/unchecked from DB.