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


i new in wpf. trying load values database fill in checkedlistbox. based on condition, items must set checked while loading in checkedlistbox. how this? have tried code below, items loaded in checkedlistbox, not checked. below values loaded checked listbox

    public void fillcheck()     {         con = new sqlconnection(connectionstring);         con.open();         string comboquery = "select [machine] department 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 refer    object[] jobs = checkedlistbox1.items.cast<object>().toarray();    foreach (object obj in jobs)    {     string query = "select [machine] department id='" + departmentid+ "'";   sqlcommand cmd = new sqlcommand(query, con);   sqldatareader rdr = cmd.executereader();    while(rdr.read())    {     string mac = rdr.getstring(0);//here 2 values(xray,ct)but shown ct checked,so how both checked     if (mac == obj.tostring())     {       int indexx = checkedlistbox1.items.indexof(mac);       if (indexx >= 0)       {         checkedlistbox1.setitemchecked(indexx, true);        }     }   }   rdr.close();  } 

you need transfer sqldatareader rdr content datatable. datatable object containing multiple rows have mentioned.

now next step, can apply foreach on datatable object iterate on rows :

foreach(datarow dr in dt.rows) {    if(yourcondition)    {       //set ischecked = true checkbox.    } } 

update :

try modifying while loop :

while (rdr.read()) {    string mac = rdr.getstring(0);    listitem li = new listitem();    li.value = "yourbindedvalue";// value database column    li.text = "yourbindedtext";// use mac if text.    int index = checkedlistbox1.items.indexof(li);    if (index >= 0)    {       checkedlistbox1.setitemchecked(index, true);    } }  

i have tested , works. have pass text , value of checkboxlistitem trying find in li object , can index if exists. make sure pass both attributes.


Comments

Popular posts from this blog

android - MPAndroidChart - How to add Annotations or images to the chart -

javascript - Add class to another page attribute using URL id - Jquery -

firefox - Where is 'webgl.osmesalib' parameter? -