Cascading DropDownList on GridView with Editing

Suprotim Agarwal posted a cascading dropdowonlist with gridview on this

The key is to use FilterExpression. It is handy without any code needed. However, it will be more useful if the solution can apply to update gridview for the cascading dropdownlist. Sadly, we still need some code to handle the update values from these dropdownlist.

Here is a sample for the implementation:

<asp:GridView ID="GridView1" runat="server" DataSourceID="SqlDataSource1" OnRowUpdating="GridView1_RowUpdating"

AutoGenerateColumns="false" DataKeyNames="CustomerId" >


<asp:CommandField ShowEditButton="True" />

<asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />

<asp:TemplateField HeaderText="Manufacture" SortExpression="Manufacture">


<asp:DropDownList ID="ddlManufacture" runat="server" AutoPostBack="True" DataTextField="Manufacture"

DataValueField="Manufacture" Width="122px" SelectedValue=’<%# Bind("Manufacture") %>

DataSourceID="SqlDataSource2" >


<asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:forumConnectionString %>"

SelectCommand="SELECT [Manufacture] FROM Manufactures"></asp:SqlDataSource>



<asp:Label ID="Label2" runat="server" Text=’<%# Bind("Manufacture") %>‘></asp:Label>



<asp:TemplateField HeaderText="Model" SortExpression="Model">



<asp:DropDownList ID="ddlModel" runat="server" DataSourceID="SqlDataSource3" DataTextField="Model" DataValueField="Model" >


<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="ddlModel" InitialValue="" ErrorMessage="Select a Model" ></asp:RequiredFieldValidator>

<asp:SqlDataSource ID="SqlDataSource3" runat="server" ConnectionString="<%$ ConnectionStrings:forumConnectionString %>"

SelectCommand="SELECT [Model],[Manufacture] FROM [Models] " FilterExpression="[Manufacture]= ‘{0}’">

<FilterParameters><asp:ControlParameter ControlID="ddlManufacture" Name="Manufacture" PropertyName="SelectedValue" Type="String" />





<asp:Label ID="Label1" runat="server" Text=’<%# Bind("Model") %>‘></asp:Label>





<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:test_for_forumConnectionString %>"

DeleteCommand="DELETE FROM [Customers] WHERE [CustomerId] = @CustomerId"

InsertCommand="INSERT INTO [Customers] ([Name], [Manufacture], [Model]) VALUES (@Name, @Manufacturer, @Model)"

SelectCommand="SELECT [CustomerId], [Name], [Manufacture], [Model] FROM [Customers] WHERE [Manufacture] IS NOT NULL AND [Model] IS NOT NULL"

UpdateCommand="UPDATE [Customers] SET [Name] = @Name, [Manufacture] = @Manufacture, [Model] = @Model WHERE [CustomerId] = @CustomerId">


<asp:Parameter Name="CustomerId" Type="Int32" />




void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)


//string strModel = ((DropDownList)((GridView)sender).Rows[e.RowIndex].FindControl("ddlModel")).SelectedItem.Value;

string strModel = ((DropDownList)((GridView)sender).Rows[e.RowIndex].FindControl("ddlModel")).SelectedValue;

string strManufacturer = ((DropDownList)((GridView)sender).Rows[e.RowIndex].FindControl("ddlManufacture")).SelectedItem.Value;


"Model"] = strModel;


"Manufacture"] = strManufacturer;

e.Cancel =


GridView gv = (GridView)sender;

for (int i = 0; i < gv.Columns.Count; i++)


DataControlFieldCell cell = gv.Rows[e.RowIndex].Cells[i] as DataControlFieldCell;

gv.Columns[i].ExtractValuesFromCell(e.NewValues, cell,

DataControlRowState.Edit, true);





2 Comments on “Cascading DropDownList on GridView with Editing”

  1. SQL says:

    Thanks Jingyang, this is very close to what I was needing, however, it doesn\’t work for my situation. In my case, I would like to be able to add the following to a second dropdownlist: SelectedValue=\'<%# Bind("Model") %>\’ When I attempt to do this, it works initially, but as soon as I select a different value from the first dropdownlist, I get the following error:"Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control"I have done a lot of searching and I think that the solution is to handle all of this with Code-behind, for example, by creating the 2nd dropdownlist in the code-behind (C#) when the selectedindexchange event fires for the first dropdownlist. I haven\’t yet written the code, but I will probably base it on what I found here: you know a simpler way, I would love to hear it. Thanks.

  2. ibrahim says:

    Thank you very much.Very usuful an article for me.Thanks.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s