19

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

Thanks for any help in advance :)

Edit : Perhaps my question was misinterpreted.

I would like to know if it is possible to databind the checkbox for each Item of CheckedListBox. I know how to databind to a source and how to programatically change the entries by iterating through the itmes. What I don't know is if it is possible to have a class which implements INotifyPropertyChanged so that when a "CheckedState" property changes the CheckedListBox updates itself.

FailedDev
  • 26,680
  • 9
  • 53
  • 73

3 Answers3

35

According to Samich's answer, Here is a full example, the binding source is an Object

private void Form1_Load(object sender, EventArgs e)
        {
            List<randomClass> lst = new List<randomClass>();

            lst.Add(new randomClass());
            lst.Add(new randomClass());
            lst.Add(new randomClass());
            lst.Add(new randomClass());
            lst.Add(new randomClass());
            lst.Add(new randomClass());

            ((ListBox)this.checkedListBox1).DataSource = lst;
            ((ListBox)this.checkedListBox1).DisplayMember = "Name";
            ((ListBox)this.checkedListBox1).ValueMember = "IsChecked";


            for (int i = 0; i < checkedListBox1.Items.Count; i++)
            {
                randomClass obj = (randomClass)checkedListBox1.Items[i];
                checkedListBox1.SetItemChecked(i, obj.IsChecked);
            }
        }
    }

    public class randomClass
    {
        public bool IsChecked { get; set; }
        public string Name { get; set; }
        public randomClass()
        {
            this.IsChecked = true;
            Name = "name1";
        }
    }

randomClass is for demonstration purposes

Rami Alshareef
  • 7,015
  • 12
  • 47
  • 75
  • 2
    I am already doing this. I am coming from WPF where I used to bind everything to properties and I don't seem to be able to add a binding to the checkListBox."isChecked" property. I know the property does not exist so how could we bind to it? I just want to make sure there is not a hidden trick which enables us to do this. – FailedDev Sep 20 '11 at 13:42
  • 1
    DisplayMember binds but ValueMember does not!?! Does not make sense. I used your for loop with success. thanks – Valamas Aug 07 '14 at 01:01
  • 9
    Of course this is not __real two-way__ databinding. – TaW Feb 09 '15 at 16:39
  • 1
    Shouldn't `ValueMember` be bound to an Id? – Olivier Jacot-Descombes Jun 30 '20 at 12:33
  • hint : Make sure your DataSource is placed first before DisplayMember =(The Description/data of your data column) and ValueMember =(the Id of your data column) – Jephren Naicker Feb 22 '23 at 08:31
7

You can find answer here: Using datasource with CheckBoxList

var checkBoxList = (ListBox)MyCheckBoxList;
checkBoxList.DataSource = dataSource;
checkBoxList.DisplayMember = "name";
checkBoxList.ValueMember = "enabled";

Make sure that the ValueMember is of type bool.

Community
  • 1
  • 1
Samich
  • 29,157
  • 6
  • 68
  • 77
3

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!