C# 以编程方式检查复选框列表中的项目,其中文本等于我想要的

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9185770/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 06:17:24  来源:igfitidea点击:

Programmatically Check an Item in Checkboxlist where text is equal to what i want

c#itemschecklistbox

提问by Peter Roche

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

在 C# 中,我试图检查 CheckBoxList 中文本等于我需要的项目。

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

如果你想要一个例子,我需要选择等于 abc 的 checklistbox 项目

采纳答案by wdavo

Assuming that the items in your CheckedListBox are strings:

假设 CheckedListBox 中的项目是字符串:

  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);
  }

回答by Jim Scott

Example based on ASP.NET CheckBoxList

基于 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");
}

回答by Dan B

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

所有功劳都归功于@Jim Scott - 只是增加了一点点。(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.

再折射一点...如果您将 CheckBoxList 作为对象传递给该方法,则可以将其重用于任何 CheckBoxList。您也可以使用文本或值。

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);`

回答by ardem

//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());

回答by prashant srivastav

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

我尝试添加动态创建的 ListItem 并分配选定的值。

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.

避免使用绑定数据源,因为它不会从 DB 绑定选中/未选中。