Click here to Skip to main content
15,906,645 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have five combo box in my project and i binded the first combo box with the Id column of my table and also other comboboxes with different column, and i want to use selectedindexchanged property of combo box in a way that if i change the combo box value in first combobox dropdown then the corresponding value of other column should populate in other 4 combobox. So please provide me the code for combo box in selected index changed

What I have tried:

C#
private void cbPRNo_SelectedIndexChanged(object sender, EventArgs e)
{
    SqlCommand command;
    SqlDataAdapter adapter = new SqlDataAdapter();
    DataSet ds = new DataSet();
    dlprobj.getcon();
    command = new SqlCommand("Select * from Purchase where Id='" + cbPRNo.SelectedIndex + "'", dlprobj.con);
    adapter.SelectCommand = command;
    adapter.Fill(ds);
    adapter.Dispose();
    command.Dispose();
    dlprobj.con.Close();
    cbStore.SelectedIndex = ds.Tables[0].Columns["BranchId"].ToString();

}
Posted
Updated 13-Dec-16 0:31am
v3
Comments
F-ES Sitecore 13-Dec-16 6:40am    
It's called "cascading dropdowns" so google that for sample code.

C#
SqlCommand command;
SqlDataAdapter adapter = new SqlDataAdapter();
DataSet ds = new DataSet();
dlprobj.getcon();
command = new SqlCommand("Select * from Purchase where Id='" + cbPRNo.SelectedIndex +
                         "'", dlprobj.con);
adapter.SelectCommand = command;
adapter.Fill(ds);
adapter.Dispose();
command.Dispose();
dlprobj.con.Close();

cbStore.DataSource = ds.Tables[0];
cbStore.ValueMember = "au_id";
cbStore.DisplayMember = "au_lname";
 
Share this answer
 
v2
Comments
SahuA 13-Dec-16 4:29am    
i want to initialize the other four comboboxes with the corresponding column value of selectedindex of combobox.
$*Developer - Vaibhav*$ 13-Dec-16 4:53am    
with same dataset or different?

SahuA 13-Dec-16 23:30pm    
i want to change the selected index of other combo boxes as the selected index of first combo box changes
 
Share this answer
 
Comments
SahuA 13-Dec-16 4:31am    
i want code to initialise all other combo boxes with the corressponding value of column
Richard MacCutchan 13-Dec-16 4:53am    
i want code
Then you need to write it, this forum is not here to do your work for you.
SahuA 13-Dec-16 23:26pm    
i don't want whole code but i simply want a single line that how column value initialise to other other code like:
ComboBox1.SelectedIndexChanged Event (System.Windows.Forms)[^]
{
combobox2.SelectedIndex = ds.Tables[0].Columns[5].ColumnName.ToString();
}

i currently using that but its not working.


Richard MacCutchan 14-Dec-16 3:51am    
What doe "not working" mean? You know how event handling works, and you have a link to the ComboBox documentation, so all you need to do is check which methods or properties will provide the data you need to extract.

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