Over a million developers have joined DZone.

Cascading DropDownList on GridView


{Editor's Note: Thanks to Tadit Dash, a new MVB at DZone. Among other things, Tadit blogs about ASP.Net and Telerik. We're pleased to have him on board as a Most Valuable Blogger. Check out his blog, Tadit Dash - The BugTrapper, here.}

Cascading DropDownLists on GridView

Cascading DropDownLists on GridView

In this Blog, we will learn how to get the current Row of GridView inside the SelectedIndexChanged Event of a DropDownList present on the GridView itself and then find the other DropDownList and bind that.


The requirement came up in one of my projects to Bind one DropDownList present on a Column based on the selection of DropDownList present on another Column. So, it was like the Cascading DropDownLists, where both the DropDownLists are present on the same GridView.

So, steps to get the solution would be like…

  1. Attach the SelectedIndexChanged Event for the first DropDownList on GridView
  2. Inside the Event, first get the GridView Row, from which the first DropDownList is selected
  3. Then from the current GridView Row, find the second DropDownList and bind it with a DataTable or something

The step which is bold is very important. Let’s go step by step.

GridView Markup

In the Markup, you can see that I have two DropDownLists Declared inside the GridView. One is for Country and another one for City. Just for demo purpose, I have hard coded the Items for Country. You can dynamically populate that using code.

Note: According to Step – 1, I have attached the OnSelectedIndexChanged Event to the CountryDropDownList.
Goal: Our goal is to populate the CityDropDownList for the particular row, when Country is selected in that row.

<asp:GridView ID="gvWithCascadingDropDownList" runat="server" AutoGenerateColumns="false">
		<asp:BoundField DataField="RowNumber" />
		<asp:TemplateField HeaderText="Country">
						ID="ddlCountry" runat="server"
					<asp:ListItem Text="-1">Select Country</asp:ListItem>
					<asp:ListItem Text="India">India</asp:ListItem>
					<asp:ListItem Text="Nepal">Nepal</asp:ListItem>
					<asp:ListItem Text="Bangladesh">Bangladesh</asp:ListItem>
					<asp:ListItem Text="Sri lanka">Sri lanka</asp:ListItem>
		<asp:TemplateField HeaderText="City">
				<asp:DropDownList ID="ddlCity" runat="server">

What’s Next?

Let’s write code inside the Country Change Event and populate the CityDropDownList.
So, according to the Step – 2

Inside the Event, first get the GridView Row, from which the first DropDownList is selected

For the above, we have to get the current CountryDropDownList first, which we can easily get by.

// Get the Country DropDownList first.
DropDownList ddlCountry = (DropDownList)sender;

Now the important Step – 3.

Then from the current GridView Row, find the second DropDownList and bind it with a DataTable or something

In order to get the Containing Row for the DropDownList, we have to use the Control.NamingContainer Property.

The naming container for a given control is the parent control above it in the hierarchy that implements the INamingContainer interface. A server control that implements this interface creates a unique namespace for the ID property values of its child server controls. You can use the NamingContainer property of a naming container’s child control to get a reference to its parent container.

So, after getting the current GridView Row, now it is just a matter of finding the CityDropDownList on that particular row and bind that with appropriate Data.

Below is the full code. I have used one Service to get the Cities Data. To use the Service, add Service Reference to your Project by right clicking on the Project in Visual Studio.

Service Reference Dialog

Service Reference Dialog

Service URL is – http://www.webservicex.net/globalweather.asmx?WSDL
protected void ddlCountry_SelectedIndexChanged(object sender, EventArgs e)
	// Get the Country DropDownList first.
	DropDownList ddlCountry = (DropDownList)sender;

	// Get the current GridView Row, from which the DropDownList is selected.
	GridViewRow currentRow = (GridViewRow)ddlCountry.NamingContainer;

	// Now let's find the City DropDownList on the same GridView Row.
	DropDownList ddlCity = (DropDownList)currentRow.FindControl("ddlCity");

	if (ddlCountry != null && ddlCountry.SelectedIndex > 0 && ddlCity != null)
		string selectedCountry = ddlCountry.SelectedValue;

		// Get the Cities from the Service.
		string xmlCities = GetCitiesByCountry(selectedCountry);

		// Let's parse the XML into DataTable.
		XmlTextReader citiesReader = new XmlTextReader(new System.IO.StringReader(xmlCities));
		DataSet dsCities = new DataSet();

		// Bind the City DropDownList.
		if (dsCities != null && dsCities.Tables.Count > 0 && dsCities.Tables[0].Rows.Count > 0)
			ddlCity.DataSource = dsCities.Tables[0];
			ddlCity.DataTextField = "City";
			ddlCity.DataValueField = "City";
	else if (ddlCity != null)

private string GetCitiesByCountry(string selectedCountry)
	GlobalWeatherServiceReference.GlobalWeatherSoapClient client = new GlobalWeatherSoapClient();
	return client.GetCitiesByCountry(selectedCountry);

Hope You Enjoyed Reading

If you have any queries, please comment below. I will come back to you. Feel free to Like and Share the Blog in Social Sites.

Thanks for reading. :)


Published at DZone with permission of Tadit Dash, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}