Programmatically Check an Item in Checkboxlist where text is equal to what I want
Asked Answered
D

5

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.

Dissatisfy answered 7/2, 2012 at 23:39 Comment(4)
Have you attempted this yourself yet? Is the interface to the CheckBoxList something you dont understand?Photooffset
Please don't prefix your titles with "C#" and such. That's what the tags are for.Mezzotint
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.Shorthorn
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.Dissatisfy
T
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);
  }
Tremml answered 7/2, 2012 at 23:52 Comment(4)
It's the value you want to match, E.G. a string literal of "MyValue"Tremml
if(index > 0) should be if(index >= 0), since IndexOf can return zero if it finds the first item in the list. It returns -1 if the value isn't found.Bertrando
Only the first item get checked how to use foreach using this method.Blida
I don't see .SetItemChecked() as an extensionSpar
G
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");
}
Gimel answered 8/2, 2012 at 0:0 Comment(4)
The FindByText function doesn't exist for me.Dissatisfy
Is this an asp.net application? Let me know what version of .NET and what environment and I would be happy to provide you specific instructions.Gimel
Hi Jim, I will be using C#.Net in Visual Studio 2010, using Framework 4.0 Client.Dissatisfy
Right, but what kind of project? Winform, Silverlight, ASP.NET...?Gimel
S
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);`
Superscription answered 6/5, 2014 at 19:10 Comment(0)
B
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());
Boldfaced answered 7/1, 2017 at 19:18 Comment(1)
Add some explanation with answer for how this answer help OP in fixing current issueUis
C
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.

Confuse answered 2/8, 2019 at 18:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.