user3181292
user3181292

Reputation: 89

how to trigger an event when an item is selected from combobox in c#.net

        string Sql_type = "select property_type_id,type_name from lk_tb_property_type";

        OleDbCommand cmd_type = new OleDbCommand(Sql_type, con);

        OleDbDataReader DR_two = cmd_type.ExecuteReader();
        DataTable table_two = new DataTable();
        table_two.Load(DR_two);

        //begin adding line
        DataRow row_two = table_two.NewRow();
        row_two["type_name"] = "Select Poperty Name";
        row_two["property_type_id"] = 0;
        table_two.Rows.InsertAt(row_two, 0);
        //end adding a line


        combo_type.DataSource = table_two;


        combo_type.DisplayMember = "type_name";
        combo_type.ValueMember = "property_type_id";
        combo_type.Text = "Select Poperty Name";

with this code i am fetching values for a combobox from database.now suppose my combobx is having 2 items named A and B..I have one more combobox...now what i want is that when user chooses item A from combobox the second combobox should display data related to item A when user chooses item B then data related to item B should be displayed...sohow to achieve this...??

Upvotes: 0

Views: 2158

Answers (4)

Alice
Alice

Reputation: 1265

You can do like these steps. First, bind data to comboBox1 (I suppose that your first ComboBox named "comboBox1", and your form named "Form1"), please make sure that your SQL query command is correct for comboBox1

private void Form1_Load(object sender, EventArgs e)
{
    OleDbConnection con = new OleDbConnection(constr);

    con.Open();
    string Sql_cust_name = "select customer_name from tb_customer";

    OleDbCommand cmd_cust_name = new OleDbCommand(Sql_cust_name, con);

    OleDbDataReader DR_cust_name = cmd_cust_name.ExecuteReader();
    DataTable table_cust_name = new DataTable();
    table_cust_name.Load(DR_cust_name);

    DataRow row_cust_name = table_cust_name.NewRow();
    row_cust_name["customer_name"] = "Select Customer Name";

    table_cust_name.Rows.InsertAt(row_cust_name, 0);

    combo_cust_name.DataSource = table_cust_name;
    combo_cust_name.DisplayMember = "customer_name";
    combo_cust_name.ValueMember = "customer_name";
    combo_cust_name.Text = "Select Customer Name";

    con.Close();
}

Next, bind data to comboBox2 (I suppose that your second ComboBox named "comboBox2"), you have to get comboBox1.SelectedValue whenever it is changed, this value will be used in the filtering for data in comboBox2, so you have to handle SelectedIndexChanged event for comboBox1, please make sure that you have this code somewhere in your project: this.comboBox1.SelectedIndexChanged += new System.EventHandler(this.comboBox1_SelectedIndexChanged);

To bind data to comboBox2 (I suppose that your second ComboBox named "comboBox2"), you have to get comboBox1.SelectedValue whenever it is changed

private void combo_cust_name_SelectedIndexChanged(object sender, EventArgs e)
{
    OleDbConnection con = new OleDbConnection(constr);

    con.Open();

    string customerName = "";
    if (combo_cust_name.SelectedValue.GetType() == typeof(DataRowView))
    {
        DataRowView selectedRow = (DataRowView)combo_cust_name.SelectedValue;
        customerName = selectedRow["customer_name"].ToString();
    }
    else
    {
        customerName = combo_cust_name.SelectedValue.ToString();
    }

    string Sql2 = "SELECT customer_number FROM tb_customer WHERE customer_name = '" + customerName + "'";
    OleDbCommand cmd_type = new OleDbCommand(Sql2, con);

    OleDbDataReader DR_two = cmd_type.ExecuteReader();
    DataTable table_two = new DataTable();
    table_two.Load(DR_two);

    DataRow row_two = table_two.NewRow();
    row_two["customer_number"] = "Select Customer Number";
    table_two.Rows.InsertAt(row_two, 0); 

    comboBox2.DataSource = table_two;
    comboBox2.DisplayMember = "customer_number";
    comboBox2.ValueMember = "customer_number";
    comboBox2.Text = "Select Customer Number"; 
}

Please correct the SQL query command as you want, but don't forget to put a correct filter like my sample code above.

Upvotes: 0

Damith
Damith

Reputation: 63105

you can fetch the data and bind it to combobox2 on SelectedIndexChanged event of combobox1

private void combobox1_SelectedIndexChanged(object sender, EventArgs e)
{
     var val = combobox1.SelectedValue;
     // fetch data from database 
     // you need to set SQL parameter value form SelectedValue

     combobox2.DataSource = ...; // set this value 
     combobox2.DisplayMember = .....; // set this value 
     combobox2.ValueMember = ....; // set this value 

}

Upvotes: 2

Vikas Rana
Vikas Rana

Reputation: 1999

In SelectedIndexChanged of combo box write your code. and make AutoPostBack = true of your combobox

Upvotes: 0

Ravi
Ravi

Reputation: 853

Please assign an event SelectedIndexChanged and AutoPostBack = true is this is a web Application in C#

Upvotes: 0

Related Questions