Ok this was a bit of a work. So first, you need to bind the main category dropdownlist (sample shown here):
protected void RowBound(object sender, GridViewRowEventArgs e)
{
if (((DropDownList)e.Row.FindControl("DropDownList1")) != null)
{
((DropDownList)e.Row.FindControl("DropDownList1")).Items.Add(new ListItem("A", "1"));
((DropDownList)e.Row.FindControl("DropDownList1")).Items.Add(new ListItem("B", "2"));
((DropDownList)e.Row.FindControl("DropDownList1")).Items.Add(new ListItem("C", "3"));
}
}
Now, On the selected index changed event of this DropDownList1, bind the data to the DropDownList2 based on what you select in DropDownList1:
protected void Change(object sender, EventArgs e)
{
DropDownList ddlzz = (DropDownList)this.FindControlRecursive(Gridview1, "DropDownList2");
if (((DropDownList)sender).SelectedValue == "1")
{
ddlzz.Items.Add(new ListItem("aaa", "10"));
ddlzz.Items.Add(new ListItem("aaaa", "100"));
}
else if (((DropDownList)sender).SelectedValue == "2")
{
ddlzz.Items.Add(new ListItem("bbb", "20"));
ddlzz.Items.Add(new ListItem("bbbb", "200"));
}
}
The main idea here being the method FindControlRecursive that works as follows:
private Control FindControlRecursive(Control root, string controlid)
{
if (root.ID == controlid)
{
return root;
}
else
{
foreach (Control ctrl in root.Controls)
{
Control ctrl2return = FindControlRecursive(ctrl, controlid);
if (ctrl2return != null)
{
return ctrl2return;
}
}
return null;
}
}
You do it recursively to find the second drop downlist which is contained in the grid view as another template column and the first dropdown would have no direct way of identifying it.
Remove the onselectedIndexChanged event from the GridView, that is for when you have a select button in your grid which you don't.
Your markup for the DropDownList1 would look like this:
<asp:TemplateField>
<ItemTemplate>
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="true" OnSelectedIndexChanged="Change"></asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
You can extend this logic to cover the third drop down as well in the same manner. Hope this helps.