Winforms : How to bind the Checkbox item of a CheckedListBox with databinding

前端 未结 3 2089
旧时难觅i
旧时难觅i 2020-12-03 17:17

I have a databinded checkedlistbox in one form and I would like to know if it is even possible to databind the check box of each list box item with a certain property of an

3条回答
  •  隐瞒了意图╮
    2020-12-03 17:54

    I just got how to databind a table in sql to a checkboxlist without stress. I am more than excited to share it. I added them manually...

            SqlConnection conn = new SqlConnection();
            SqlCommand cmd = new SqlCommand();
            conn.ConnectionString = "Data Source=MICMIKE\\SQLEXPRESS;Initial Catalog=Enterprise;Integrated Security=True";
            conn.Open();
            string query = "Select Position from Position";// position column from position table
            cmd.Connection = conn;
            cmd.CommandText = query;
    
            SqlDataReader dr = cmd.ExecuteReader();
            while (dr.Read())
            {
                string myItem = dr["Position"].ToString();
                checkedListBox1.Items.Add(myItem, true);//true means check the items. use false if you don't want to check the items or simply .....Items.Add(myItem);
            }
    

    To Access the items checked in the checklistbox, use

            foreach(object item in Checkedlistbox1.CheckedItems)
            {
                 string itemchecked = item.ToString();
                 MessageBox.Show(itemchecked);// This will show all the checked items in the checklistbox.
            }
    

    It is really working. I just got it now. I hope you like it!

提交回复
热议问题