How to bind a checkedlistbox to a combobox and a textbox at the same time in C#?

Ramtin 1 Reputation point
2020-09-14T16:18:46.927+00:00

I have a database in ms access. I have connected it to C#.
The user needs to select a crane type through a combobox. The moment that the user selects the item in the combobox I want to have some items related to that choice in a checkedlistbox, which I have done this part. The next step which I do not know how to do is that to refine the items in the checkedlistbox, there are 2 textboxes for minimum and maximum capacity of the crane. When user enters the minimum number I want the checkedlistbox updated with those crane that have a greater capacity than the value in the first testbox and again when the user enters the maximum capacity in the second textbox the last filter applied the final items populated in the checkedlistbox.

This is my table:

| Crane Index | Crane Model Number | Crane Type | Crane Capacity Rating (tons) |

|:----------- ----:|:------------------------:|:-------------:|:--------------------------------:|
| 221 | LR 1400-1 | Crawler | 440 |
| 258 | CC 2800 | Crawler | 660 |
| 262 | CC 2400-1 | All Terrain | 400 |
| 265 | CC 6800 | Crawler | 1375 |
| 277 | LR 11350 | All Terrain | 1250 |

What I have tried:

<pre>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;


private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
       {
           //clear the checkedboxlist everytime that the combobox itme is changed
           checkedListBoxCrane.Items.Clear();
           //loop through the "cranemaintable" table and if the item that selected in the combobox is
           //equal to the "crane type" column then populate that item into the checkedlistbox
           foreach(DataRow item in test.Tables["cranemaintable"].Rows)
           {
               if (Convert.ToString(item["crane type"]) == Convert.ToString(comboBox1.SelectedItem))
               {
                   checkedListBoxCrane.Items.Add(Convert.ToString(item["crane model number"]));
               }

           }

       }
Windows Forms
Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,860 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Daniel Zhang-MSFT 9,621 Reputation points
    2020-09-15T09:56:21.56+00:00

    Hi Ramtin,
    To achieve the final requirement, you can use the values of textbox1, textbox2 and combobox as conditions to fill the checkedlistbox with corresponding data.

    private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
    
        //clear the checkedboxlist everytime that the combobox itme is changed
        checkedListBox1.Items.Clear();
        //loop through the "cranemaintable" table and if the item that selected in the combobox is
        //equal to the "crane type" column then populate that item into the checkedlistbox
    
        foreach (DataRow item in dt.Rows)
        {
            if (Convert.ToString(item["crane type"]) == Convert.ToString(comboBox1.SelectedItem))
            {
    
                if (Convert.ToInt32(item["Crane Capacity Rating (tons)"]) > int.Parse(textBox1.Text) && Convert.ToInt32(item["Crane Capacity Rating (tons)"]) < int.Parse(textBox2.Text))
                {
    
                    checkedListBox1.Items.Add(Convert.ToString(item["crane model number"]));
    
                }
    
            }
    
        }
    }
    private void textBox2_TextChanged(object sender, EventArgs e)
    {
        comboBox1.DataSource = crane_type;
    
    }
    

    Best Regards,
    Daniel Zhang

    0 comments No comments