Gabriel olayinka

Gabriel olayinka

  • NA
  • 119
  • 4.7k

Checking and unchecking a checklistbox base on combo box val

Mar 28 2018 4:12 AM
i and having a combo box which contain a valueMember for each item in the combo box.....
i am also having a checklist box which also contain some item ...... what i want to do is to check and uncheck the checklistbox base on the selectedValue of the combobox....
 
here is my code
  1. int STAFF_IDs = (int)txtTeacher.SelectedValue;  
  2. con.Open();  
  3. SqlDataAdapter ds1 = new SqlDataAdapter("select * FROM SUBJECT_STAFF where STAFF_ID= '" + STAFF_IDs + "' ", con);  
  4. DataTable dt1 = new DataTable();  
  5. ds1.Fill(dt1);  
  6. if (dt1 != null && dt1.Rows.Count > 0)  
  7. {  
  8. for (int i = 0; i < checkedListBox1.Items.Count; i++)  
  9. {  
  10. for (int k =0; i< dt1.Rows.Count; i++)  
  11. {  
  12. checkedListBox1.SetItemCheckState(i, (STAFF_ID == Convert.ToInt32(dt1.Rows[k]["STAFF_ID"]) ? CheckState.Checked : CheckState.Unchecked));  
  13. } }//end for  
  14. }//end if  
the problem i am having is that the checklistbox is not checking the appropriate items in the checklistbox ... again i'm having problem saving all the checked item to the database at once
 
(NOTE: the purpose of the code is to have a combo box that contain staff name...... and a checklistbox that contain the list of subject..... then i want to assign subjects to staffs from the checklistbox..... again whenever a staff is selected from the combobox, i want the subject that has been assigned to the particular staff be checked on the checklistbox) 

Answers (3)