tariq
tariq

Reputation: 43

Select the contain for 2nd combobox depending on the select from the 1st combobox

I have two table 1st table name (X) with ( class ) column. And the 2nd table name (Y) with (name) column, like this:

Table (X):- inserted in class column ( Class A , Class B ).

Table (Y):- Inserted the name of student in the name column( Jon, Mary, Bob, tar, Mike )

Then I used two comboboxes; the first combobox contain Table (X) (i.e Class A , Class B ) and the 2nd combobox contain the table (Y) (i.e the name of student Jon, Mary, Bob, tar, Mike).

I want when I choose from the first combobox Class A, the 2nd combobox appear only (Jon, Mary), and if I choose from the first combobox Class B, the 2nd combobox just appear to me (tar, Mike, Bob).

I used this function to fill the first combobox:

void fillcombo()
{
    string Coonstring = "datasource=localhost;port=3306;username=root;password=***;Charset=utf8";
    string cmd = "select class from project.X ;";
    MySqlConnection connectionDatabase = new MySqlConnection(Coonstring);
    MySqlCommand cmddata = new MySqlCommand(cmd, connectionDatabase);
    MySqlDataReader myreader;

    try
    {
        connectionDatabase.Open();
        myreader = cmddata.ExecuteReader();
        while (myreader.Read())
        {
            string sname = myreader.GetString("class");
            comboBox1.Items.Add(sname);
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}

So , I have 2 combo boxes. I want to set it up such that when the user chooses an information on the first combo box, that would determine what is to be displayed on the second combobox. How can do that's .

Upvotes: 0

Views: 1020

Answers (2)

Smack
Smack

Reputation: 946

You can easily bind contents of 2nd dropdown based on value selected for first dropdown i have assumed your first dropdown is ddlClass and 2nd to be ddlNames. You can refer following code snippet. Hope this helps.

protected void ddlClass_SelectedIndexChanged(object sender, EventArgs e)
        {
            // Remove Names dropdownlist items 
            ddlNames.Items.Clear();
            string strClass = string.Empty;
            ComboBox comboBox = (ComboBox) sender;
    strClass= (string) ddlClass.SelectedItem;
            List<string> list = null;

            // Bind Names dropdownlist based on Class value 
            list = GetNamesByClass(strClass);
            ddlNames.DataSource = list;



        }

        private List<string> GetNamesByClass(string clsss)
        {
            //Your database code to get names list based on class goes here
            //have to write code to get 2nd dropdown data here

        }

I would recommend you not to write your database related code in code behind rather to use layered architecture so that your code is maintainable and avoids code duplication. You can go through 3 tier architecture where your code is dividend into

  1. BAL- business Access layer- Your busines rules can be put in here
  2. DAL- your database logic goes here fetch data from database
  3. code behind and UI- code behind only contains reference to BAL and DAL and you can have aspx file for your UI

Upvotes: 1

Nahuel Ianni
Nahuel Ianni

Reputation: 3185

You can use the "SelectionChanged" event of the first combo box. It will be fired every time a user selects a new value on the combo, and you have in the event parameters the item that was actually selected, so it will be easy to implement the logic of filling the second combo box from there.

If you are using XAML, a similar approach can be used when binding a property to the "SelectedItem" control and then putting the logic in the VM.

Upvotes: 0

Related Questions