Click here to Skip to main content
15,886,091 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more: , +
I have database in ms access. I have connected it to C#.
The user needs to select a crane type through a combobox. The moment that 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 and again when user enters the maximum capacity in the second textbox the last filter applied the final items populated in the checkedlistbox.

This is my table in the database

| 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:

C#
<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;


C#
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"]));
               }

           }

       }
Posted
Updated 14-Sep-20 6:31am
Comments
[no name] 13-Sep-20 10:46am    
In this case, you're better off setting the checkboxes when you "save", or try to save. Because, you also need to "uncheck" if the user starts reverting their original choices.Sounds like some "completed checklist" so checking "at the end" is not uncommon.
Ramtin992 13-Sep-20 15:19pm    
Could you please explain a little more? I am pretty new to c#
Thank you.

1 solution

I'm not going to write our code for you, but, I hope I can give you some ideas:

1 to fill the ComboBox with a set of nom duplicate items:
// dt is the DataTable
// c2 is the #2 DataColumn

var cval = dt.AsEnumerable().Select(row => row[c2]).Distinct().Select(rw => rw.ToString()).ToList();

comboBox1.DataSource = cval;
2 filling the CheckdListBox when the ComboBox selection changes:
private void comboBox1_SelectedValueChanged(object sender, EventArgs e)
{
    checkedListBox1.Items.Clear();

    string csel = comboBox1.SelectedValue.ToString();

    foreach(DataRow row in dt.Rows)
    {
        if (row[2] == csel)
        {
            checkedListBox1.Items.Add($"{row[1]}\t\tMax Tons: {row[3]}");
        }
    }
}
Here, I made a choice to append the max tons to the string displayed in the CheckedListBox.

3 At this point we come to your goal to filter the CheckedListBoxItems by minimum and maximum capacity values. I suggest you use NumericUpDown Controls for user input.

It's too bad that CheckedListBoxItems do not have a 'Tag property you could stick the max capacity in. I suggest you create a reference Dictionary to map the ComboBoxItems to the combination of (perhaps a Tuple ?) of CraneModel and max capacity: then, when the user changes the capacity, min, or max, you can iterate this Dictionary to rebuild/update the CheckedListBoxItems,

// sketch Dictionary<comboboxitem, list<(?,="" ?.capacity)="">

Does the end-user really need to enter both min and max ?
 
Share this answer
 
Comments
Ramtin992 14-Sep-20 13:22pm    
Thanks for your response.
No, the user does not have to put a number there, it is optional to refine the results if he or she wants.

I got an error for this section:
string csel = comboBox1.SelectedValue.ToString();
it says: Object reference not set to an instance of an object
How can I fix this? I know the reason but how can I fix it?
I have comboBox1.SelectedItem = null; as I want the combobox be null at the beginning of the program.
BillWoodruff 14-Sep-20 17:25pm    
Now is the good part :) where you learn to debug. Start by putting breakpoints in the ComboBox: if you never hit the breakpoint, then you haven't wired up the SelectedValueChanged eventhandler for the ComboBox.

if the ComboBox throws an error on string csel = comboBox1.SelectedValue.ToString();

Then use a breakpoint before that line and examine if you have actually added the values to the ComboBox.

The code examples I showed you are all dependent on the DataTable being fully configured. GThe first example requiress you have an instance of a DataColumn in a variable 'c3.
Ramtin992 14-Sep-20 21:06pm    
Thanks again for your help. I could fix it.
I should mention that I really liked your idea.
Also this part is great :
checkedListBox1.Items.Add($"{row[1]}\t\tMax Tons: {row[3]}");
There is a weird problem here, it is working but in a weird way
There are 13 items that populate in the checkelistbox and all of them are aligned with eachother except for two of them, it seems that those to items do not accept the " \t\t" in the code and attache to the {row[1]}.
If it happened for all of them I thought that I have done something wrong, but it is just for 2 items that happend always. Those two does not align with the rest of the items.
BillWoodruff 15-Sep-20 5:14am    
hypothesis: the /t is a tab character ... probably the line was cut off because it was too long to fit the boundaries of the CheckedListBox control ... try replacing the tabs with a few spaces and see ...
Ramtin992 15-Sep-20 9:48am    
I have done this:
checkedListBox1.Items.Add($"{row[1]}>>\tMax Tons: {row[3]}");
It is working perfectly.
Thank you.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900