Search code examples
c#ms-accesscomboboxcheckedlistbox

How to bind a checkedlistbox to a combobox in C#?


I have a combobox that user can select an Item and based on that item in the checkedlistbox some other items need to be populated.

  1. I can bind the combobox to a column in my database when all the values in the column are different. However, in this case in that column there are duplicated items and I do not want to have multiple items of the same type. I just want to have one "Crawler" and one "All Terrain" in the combobox.

This is the code that used to use to bind source to a combobox, but does not work for this case.

comboBox1.ValueMember = "crane index";
comboBox1.DisplayMember = "crane type";
comboBox1.DataSource = test.Tables["cranemaintable"];

this the table in the database

| Crane Index | Crane Model Number |  Crane Type |
|:-----------:|:------------------:|:-----------:|
| 221         | LR 1400-1          | Crawler     |
| 258         | CC 2800            | Crawler     |
| 262         | CC 2400-1          | All Terrain |
| 265         | CC 6800            | Crawler     |
| 277         | LR 11350           | All Terrain |
  1. Database: MS Access
  2. Dataset: test
  3. Name of the table: cranemaintable

I have checked the other questions related to mine but could not find an answer to my problem. Your help will be appreciated. Thanks.

Update: This is what I have found and worked for me.

// create a list that holds all the crane types but it should not have duplicated items
        List<string> crane_type = new List<string>();
        for (int i = 0; i< test.Tables["cranemaintable"].Rows.Count; i++)
        {
            if (!crane_type.Contains(test.Tables["cranemaintable"].Rows[i]["crane type"]))
            {
                crane_type.Add(Convert.ToString(test.Tables["cranemaintable"].Rows[i]["crane type"]));
            }                
        }        
        //bind the crane_type list to the combobox1
        comboBox1.DataSource = crane_type;

Solution

  • Ok first my answer is ugly but it works and its the only one I know, so we start with you importing your databasetabe into a list this is a class you can use

    public class myclass
       {
          public string CraneIndex { get; set; }
          public string CraneModelNumber { get; set; }
          public string CraneType { get; set; }
       }
    

    and then make 3 lists

      List<myclass> myclasses = new List<myclass>();
      List<myclass> myclasses1 = new List<myclass>();
      List<myclass> myclasses2 = new List<myclass>();
    

    here you import your table into the list myclasses and then we filter the main list that came from the database to only have unique types

          myclasses1 = myclasses.GroupBy(myclass => myclass.CraneType).Select(x => x.First()).ToList();
          comboBox1.DataSource = myclasses1;
          comboBox1.DisplayMember = "CraneType";
    

    go to the combobox and subscribe to the selectedindexchanged property and add this into its function

     myclasses2.Clear();
        foreach (var item in myclasses)
        {
           if (comboBox1.Text==item.CraneType)
           {
             myclasses2.Add(item);
           }
        }
        checkedListBox1.Items.Clear();
        foreach (var item in myclasses2)
        {
            checkedListBox1.Items.Add(item.CraneModelNumber);
        }
        this.checkedListBox1.DisplayMember = "cranemodels";
        checkedListBox1.Refresh();
    

    Let me know if you need any more help or explanation.