Resolved Help Needed Please (comboBox Items)

SteveLincs87

New member
Joined
Jun 6, 2022
Messages
2
Programming Experience
Beginner
Hello everyone,
Hope your well! I'm really new to C# I've not had formal training on it and what I do know is self taught to a degree. There's something about designing and building a working program that has a use that I just love. So I've recently started a new work role in sales. I'm having to deal with various price lists etc so during my spare time I'm fulfilling my hobby and trying to design a program that helps me at work. As I don't know a great deal I've joined this forum to gain knowledge and hopefully one day contribute. So my current issue relating to C# is with a combo box. My form has 2 combo boxes the first you select a product and the second you select the size. What I'm wanting is the 2nd combo box items to change depending on what is selected in the first combo box if that makes sense. Sizes vary depending on what product is selected in combo box 1 if a certain product is selected I want the 2nd combo box items to change if needed to suit the product selected. If that's at all possible...Any help would be massively appreciated.
 
Whenever the selected item changes on the first combobox, change the items available in the second combobox.
 
Whenever the selected item changes on the first combobox, change the items available in the second combobox.

This is what i've gone with, probably not the best way to do things but seems to be working :) thankyou for the quick reply, appreciated :)

What I've done:
                if (comboBox1.Text == "Product")
                {
                comboBox2.Items.Clear();
                comboBox2.Items.Add("Size");
                }
 
This is easily achievable using data-binding if the data is stored in DataTables in a DataSet. You can create a DataRelation between the two tables and bind the child control to that. Selection of a parent will then automatically filter the child list.
  1. Create a new Windows Forms Application project.
  2. Add two ComboBoxes to the form.
  3. Add two BindingSources to the form.
  4. Add the following code:
C#:
private void Form1_Load(object sender, EventArgs e)
{
    // Get the data.  The DataSet must contain a Parent table,
    // a Child table and a ParentChild relation between them.
    DataSet data = this.GetDataSet();

    // Bind the parent source to the parent table.
    this.bindingSource1.DataSource = data;
    this.bindingSource1.DataMember = "Parent";

    // Bind the child source to the relationship.
    this.bindingSource2.DataSource = this.bindingSource1;
    this.bindingSource2.DataMember = "ParentChild";

    // Bind the parent control to the parent source.
    this.comboBox1.DisplayMember = "Name";
    this.comboBox1.ValueMember = "ID";
    this.comboBox1.DataSource = this.bindingSource1;

    // Bind the child control to the child source.
    this.comboBox2.DisplayMember = "Name";
    this.comboBox2.ValueMember = "ID";
    this.comboBox2.DataSource = this.bindingSource2;
}

private DataSet GetDataSet()
{
    DataSet data = new DataSet();
    DataTable parent = this.GetParentTable();
    DataTable child = this.GetChildTable();

    data.Tables.Add(parent);
    data.Tables.Add(child);

    // Add a relationship between the ID of the parent
    // table and the ParentID of the child table.
    data.Relations.Add("ParentChild",
                       parent.Columns["ID"],
                       child.Columns["ParentID"]);

    return data;
}

private DataTable GetParentTable()
{
    DataTable table = new DataTable("Parent");

    table.Columns.Add("ID", typeof(int));
    table.Columns.Add("Name", typeof(string));

    table.Rows.Add(1, "Parent1");
    table.Rows.Add(2, "Parent2");
    table.Rows.Add(3, "Parent3");

    return table;
}

private DataTable GetChildTable()
{
    DataTable table = new DataTable("Child");

    table.Columns.Add("ID", typeof(int));
    table.Columns.Add("ParentID", typeof(int));
    table.Columns.Add("Name", typeof(string));

    table.Rows.Add(1, 1, "Child1A");
    table.Rows.Add(2, 1, "Child1B");
    table.Rows.Add(3, 1, "Child1C");
    table.Rows.Add(4, 2, "Child2A");
    table.Rows.Add(5, 2, "Child2B");
    table.Rows.Add(6, 2, "Child2C");
    table.Rows.Add(7, 3, "Child3A");
    table.Rows.Add(8, 3, "Child3B");
    table.Rows.Add(9, 3, "Child3C");

    return table;
}
Make sure that the Load event handler is actually linked to the Load event of the form. Now run the project and make selections from the parent list in the first ComboBox. Watch the child list filter automatically as you do.

This example obviously populates the DataTables manually but it works the same way for DataTables populated from related tables in a database, which is how you would normally store such data.
 
Back
Top Bottom