Introduction

This article demonstrates how to bind a DropDownList inside a GridView, and also shows how to bind a TextBox inside a GridView on the SelectedIndexChanged event of the DropDownList.

Using the Code

To start with, add the GridView control to your page, add the bound columns if any, then add two template columns: one for the DropDownList and one for the TextBox. Set the AutoPostBack property to "True" for the DropDownList, and create the SelectedIndexChanged event.

The first thing to do is to bind the gird with some sample data. For the sake of simplicity, I am binding the sample data to the grid. For this, I am creating an object of the Customer class, assigning its properties with dummy data, and adding it to the list. Here is the code for this:

The second step is to bind the DropDownList with data. To achieve this, we have to use the RowDataBound event of the GridView. This event fires when we bind the data to the GridView. In this event, we need to find the DropDownList control of each row and bind it with some data. Again, for the sake of simplicity, I am binding it with dummy data by creating an object of the dummy class called "DropDownData" and adding these objects to the list, and finally, assigning this list as a datasource to the DropDownList:

The third and most important step is to handle the SelectedIndexChanged event of the DropDownList. As we have to identify the exact Row of the GridView from where the SelectedIndexChanged event is fired, we need to compare the "ClientID" of the DropDownList with the dropdowns available in all the rows of the GridView. Once the CliendID is matched, we can bind the TextBox with some data of that Row. For simplicity, I am binding the DropDownList's SelectedValue data. You can fire a database query to get real data as well. Here is the code:

There needs to be more explanation of the code to make this a proper article. Do not rely on others being able to understand which line of code does what, try and explain it to them with details about why it does what it does.