【问题标题】:How to retrieve data from database to CheckedListBox and set the items as checked?如何从数据库检索数据到 CheckedListBox 并将项目设置为选中?
【发布时间】:2015-05-12 04:35:46
【问题描述】:

我是 WPF 的新手。我正在尝试从数据库加载值以填写CheckedListBox。根据条件,某些项目在加载到checkedlistbox 时必须设置为已选中。 这该怎么做?我试过下面的代码,项目被加载到CheckedListBox,但没有被检查。 下面是加载到选中列表框的值

    public void fillcheck()
    {
        con = new SqlConnection(connectionstring);
        con.Open();
        string comboquery = "SELECT [Machine] FROM Department Where active='True'";
        SqlCommand cmd = new SqlCommand(comboquery, con);
        SqlDataReader rdr = cmd.ExecuteReader();
        while (rdr.Read())
        {
            string fil1 = rdr.GetString(0);
            Checkedlistbox1.Items.Add(fil1);
        }
        rdr.Close();

     }




    int departmentID=60//for just refer
   Object[] jobs = CheckedlistBox1.Items.Cast<Object>().ToArray();
   foreach (Object obj in jobs)
   {
    string query = "SELECT [Machine] FROM Department Where ID='" + departmentID+ "'";
  SqlCommand cmd = new SqlCommand(query, con);
  SqlDataReader rdr = cmd.ExecuteReader();
   while(rdr.Read())
   {
    string mac = rdr.GetString(0);//Here i get two values(XRAY,CT)but finally shown CT only be checked,so how to do both checked
    if (mac == obj.ToString())
    {
      int indexx = CheckedlistBox1.Items.IndexOf(mac);
      if (indexx >= 0)
      {
        CheckedlistBox1.SetItemChecked(indexx, true);
       }
    }
  }
  rdr.Close();
 }

【问题讨论】:

  • 可能您想迭代 chlstBox.Items 中的所有项目而不是 chlstBox.CheckedIndices?
  • 是的,值已加载到checkedlistbox中,但根据我上面提到的情况,我无法检查
  • 您应该使用 code-foreach (int indexChecked in chlstBox.Items) 而不是 foreach (int indexChecked in chlstBox.CheckedIndices)。开始时,您有 0 个选定项目。

标签: c# sql-server wpf foreach checkedlistbox


【解决方案1】:

您需要将您的 SqlDataReader rdr 内容传输到 DataTable。这将帮助您获得一个包含多行的 DataTable 对象,就像您提到的那样。

现在进行下一步,您可以在该 DataTable 对象上应用 foreach 以迭代其所有行,如下所示:

foreach(DataRow dr in dt.Rows)
{
   if(yourCondition)
   {
      //set isChecked = true for the checkbox.
   }
}

更新:

尝试像这样修改您的 while 循环:

while (rdr.Read())
{
   string mac = rdr.GetString(0);
   ListItem li = new ListItem();
   li.Value = "yourBindedValue";// some value from database column
   li.Text = "yourBindedText";// use mac if its text.
   int index = Checkedlistbox1.Items.IndexOf(li);
   if (index >= 0)
   {
      Checkedlistbox1.SetItemChecked(index, true);
   }
} 

我已经对此进行了测试,并且可以正常工作。您只需要传递您试图在li 对象中查找的CheckBoxListItem 的TextValue,如果它存在,您就可以获得索引。确保传递两个属性。

【讨论】:

【解决方案2】:

你应该用过代码-

foreach (int indexChecked in chlstBox.Items)

而不是

foreach (int indexChecked in chlstBox.CheckedIndices)

一开始你有 0 个选定的项目,这就是为什么你的外部 for 循环不起作用..

编辑-

基本逻辑也不正确。 您应该遍历数据集,在复选框列表中找到字符串,然后检查它。因此,不需要外部 foreach 循环。此外,请确保您使用正确的 checkboxlist 变量。在 for 循环中,您正在使用 chlstBox 并且在搜索时您正在使用 Checkedlistbox1 ....

【讨论】:

  • foreach (int indexChecked in chlstBox.Items),这是获取值,但不能检查,总是报错“这个枚举器绑定的列表已经被修改。一个枚举器只能是如果列表没有变化,则使用。"
  • 请看我的编辑。您不需要外部 foreach 循环。
  • 首先,展示完整的代码。也显示 fillcheckedlist 的代码。此外,HarveySpecter 解决方案应该可以工作。
猜你喜欢
  • 1970-01-01
  • 2017-09-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-10-23
  • 2018-02-26
相关资源
最近更新 更多