简体   繁体   中英

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

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

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

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

Now for the next step, you can apply a foreach on that DataTable object to iterate over all its rows like this :

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

UPDATE :

Try modifying your while loop like this :

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 Text and Value of the CheckBoxListItem that you are trying to find in the li object and you can get the index if it exists. Make sure you pass both the attributes.

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..

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 chlstBox and while searching you are using Checkedlistbox1 ....

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM