wpf 如何从数据库检索数据到 CheckedListBox 并将项目设置为已选中?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30181869/
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-09-13 13:07:38  来源:igfitidea点击:

How to retrieve data from database to CheckedListBox and set the items as checked?

c#sql-serverwpfforeachcheckedlistbox

提问by Ramji21

I am new in WPF. I am trying to load the values from database to fill in CheckedListBox. Based on a condition, some items must be set to checked while loading in checkedlistbox. How to do this? I have tried the code below, items are loaded in CheckedListBox, but are not checked. Below is values loaded to checked listbox

我是 WPF 的新手。我正在尝试从数据库加载值以填充CheckedListBox. 根据条件,某些项目在加载到checkedlistbox 时必须设置为checked。这该怎么做?我已经尝试了下面的代码,项目已加载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();
 }

回答by Matt Murdock

You need to transfer your SqlDataReader rdrcontent to a DataTable. That will help you get a DataTable object containing multiple rows like you have mentioned.

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

Now for the next step, you can apply a foreachon that DataTableobject to iterate over all its rows like this :

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

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

UPDATE :

更新 :

Try modifying your whileloop like this :

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

I have tested this and it works. You just have to pass the Textand Valueof the CheckBoxListItem that you are trying to find in the liobject and you can get the index if it exists. Make sure you pass boththe attributes.

我已经测试过这个并且它有效。您只需要传递您试图在对象中查找的 CheckBoxListItem的TextValueli如果它存在,您就可以获得索引。确保您传递了这两个属性。

回答by Amit

You should have used code-

您应该使用过代码-

foreach (int indexChecked in chlstBox.Items)

instead of

代替

foreach (int indexChecked in chlstBox.CheckedIndices)

At start you have 0 selected items and thats why your outer for loop is not working..

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

EDIT-

编辑-

Basic Logic is also incorrect. You should loop through dataset, find the string in checkboxlist and then check it. So, outer foreach loop is not required. Also, make sure that you are using correct checkboxlist variable. In for loop you are using chlstBoxand while searching you are using Checkedlistbox1....

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