Hi,
i have 3 combobox columns in datagridview,when i change value in 1st combobox column
then 2nd combobox column should fill with corresponding values,and by changing 2nd combobox value then 3rd combobox column should fill with corresponding values.how can i do this?
Ex:
first combo box column: Category
display members: A,B,C
second combo box column:Products
display members:a,a1,a2 b,b1,b2 c,c1,c2
when i select 'A' in combobox1 ,then 2nd combobox should contain\display a,a1,a2 only not others
and when i select 'B' in combobox1 ,then 2nd combobox should contain\display b,b1,b2 only not others
Plese answer if anyone have solution
thanks
Loading

TulasiRank #2,544 · 549 reputationPosted Oct 13, 2011, 3:35 AM EDT
have a look at this code
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
List
lst.Add("Row1");
grdcmb.DataSource = lst.ToArray();
grdcmb.DataBind();
}
}
protected void drpCatagory_ChangedEvent(object sender, EventArgs e)
{
DropDownList prod_dr = (DropDownList)grdcmb.Rows[0].FindControl("drpProduct");
DropDownList drptest = (DropDownList)sender;
if (drptest.SelectedItem.Text == "A")
{
List
prod_lst.Add("a1");
prod_lst.Add("a2");
prod_lst.Add("a3");
prod_dr.DataSource = prod_lst.ToArray();
prod_dr.DataBind();
}
else if (drptest.SelectedItem.Text == "B")
{
List
prod_lst.Add("b1");
prod_lst.Add("b2");
prod_lst.Add("b3");
prod_dr.DataSource = prod_lst.ToArray();
prod_dr.DataBind();
}
else if (drptest.SelectedItem.Text == "C")
{
List
prod_lst.Add("c1");
prod_lst.Add("c2");
prod_lst.Add("c3");
prod_dr.DataSource = prod_lst.ToArray();
prod_dr.DataBind();
}
else
{
List
prod_dr.DataSource = prod_lst.ToArray();
prod_dr.DataBind();
}
}
protected void grdcmb_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
DropDownList test =(DropDownList) e.Row.FindControl("drpCatagory");
List
cat_lst.Add("Select");
cat_lst.Add("A");
cat_lst.Add("B");
cat_lst.Add("C");
test.DataSource = cat_lst.ToArray();
test.DataBind();
}
}
Please do not forget to mark "Accpeted Answer".
Kurt Bugeja6 reputationPosted Oct 13, 2011, 3:48 AM EDT
For an efficient way of representing this logic:
1. Handle the onSelectionChanged event from parent combobox.
2. Declare and initialize an arraylist of strings: the first element to hold the values of the parent combobox and the second element to hold the corresponding values of the child combobox. Ex:
A | a
A | a1
A | a2
B | b
B | b1
...
3. Implement a switch case statement to switch amongst parent combobox values on the selected value.
4. If ex: the selected value is A therefore the "case A:" clause is executed, visible only those values in the child combobox which have the value corresponding to the values in the second element of the arraylist with the associated selected value from the parent combobox in the first element.
This could be done using a for each loop checking both the first element of the array list and the accepting the associated values of the child combobox.
bala5 reputationPosted Oct 13, 2011, 4:55 AM EDT
but i want this for Windows application
Kurt Bugeja6 reputationPosted Oct 13, 2011, 4:56 AM EDT
bala5 reputationPosted Oct 13, 2011, 5:11 AM EDT
these comboboxes are in datagridview,
can u please tell me in which event/events of Datagridview i need to write the code
Kurt Bugeja6 reputationPosted Oct 13, 2011, 6:48 AM EDT
// Implement the event EditingControlShowing of the DataGridView
private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
if (dataGridView1.CurrentCell.ColumnIndex == 0)
{
// Check box column.
ComboBox combobox = e.Control as ComboBox;
combobox.SelectedIndexChanged += new EventHandler(combobox_SelectedIndexChanged);
}
}
private void combobox_SelectedIndexChanged(object sender, EventArgs e)
{
string selectedText = ((ComboBox)sender).SelectedText;
MessageBox.Show("Selected Text: {0}", selectedText);
}