<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns=false OnSelectedIndexChanged="GridView1_SelectedIndexChanged" OnSelectedIndexChanging="GridView1_SelectedIndexChanging"> <Columns> <asp:TemplateField> <HeaderTemplate> DropDown 1 </HeaderTemplate> <ItemTemplate> <asp:DropDownList ID="dd1" runat=server DataSource='<%#Load_DropDown()>' DataTextField="brand_name" DataValueField="brand_id" OnSelectedIndexChanged="Populate_Grid2" AutoPostBack=true> </asp:DropDownList> </ItemTemplate> </asp:TemplateField> <asp:TemplateField> <HeaderTemplate> DropDown 2 </HeaderTemplate> <ItemTemplate> <asp:DropDownList ID="dd2" runat=server DataTextField="brand_name" DataValueField="brand_id"></asp:DropDownList> </ItemTemplate> </asp:TemplateField> </Columns> </asp:GridView>
//function to determine which one is selected in dd1 protected void Populate_Grid2(object sender, EventArgs e) { //locate the row in which the dropdown value has been changed GridViewRow gr=(GridViewRow)((DataControlFieldCell)((DropDownList)sender).Parent).Parent; //find the control in that DropDownList d1=(DropDownList) gr.FindControl("dd1"); string selectedvalue=d1.selectedvalue; //using selectedvalue execute a query like //select * from product where brand_id=selectevalue //get the result in datatable dt //located the second dropdown(dd2) DropDownList d2 = (DropDownList)gr.FindControl("dd2"); d2.DataSource = dt; d2.DataBind(); }