DropDownList controls in a Gridview

The sample code load values in dropdown2(dd2) based on the selected value in dropdown1(dd1)

The code below is that of an .aspx page that contains the DropDownList controls in a GridView control


<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>


The code behind page handles the event that populates data in 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();

}


Comments

Author: Ajay kumar11 Nov 2008 Member Level: Gold   Points : 1

Thanks friend very much. This code helped me a lot.

Keep posting.

God bless u.

Happy Programming.

Thanks again for million times.

Guest Author: nandakumar25 Apr 2012

Thank you very much dude.You helped by without siting overnite

Guest Author: Dieu Hau Con15 Jun 2012

Thanks you very much.It's very useful.



  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: