In this article I will explain with an example, how to bind ASP.Net DropDownList in ItemTemplate of GridView.
 
 

Database

Here I am making use of Microsoft’s Northwind Database. You can download it from here.
 
 

HTML Markup

The following HTML Markup consists of:
GridView - For displaying data.

Columns

There are one BoundField and TemplateField columns.
TemplateField – The TemplateField column consists of ItemTemplate
ItemTemplate consists of label control and DropDownList.
<asp:GridView ID="gvCustomers" runat="server" AutoGenerateColumns="false" OnRowDataBound="OnRowDataBound">
    <Columns>
        <asp:BoundField HeaderText="Name" DataField="ContactName" />
        <asp:TemplateField HeaderText="Country">
            <ItemTemplate>
                <asp:Label ID="lblCountry" runat="server" Text='<%# Eval("Country")%>' Visible="false" />
                <asp:DropDownList ID="ddlCountries" runat="server">
                </asp:DropDownList>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>
 
 

Binding the GridView

Inside the Page_Load event handler, the GetData is assigned to the DataSource property of GridView and the GridView is populated.
Inside the GetData method, first the connection string is read from the Web.Config file.
Note: For more details on how to read connection string from Web.Config file, please refer my article Read or Write Connection Strings in Web.Config file using ASP.Net using C# and VB.Net.
 
Finally, the records are fetched from the Customers Table of SQL Server database using SqlDataAdapter.
C#
protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        gvCustomers.DataSource = GetData("SELECT ContactName, Country FROM Customers");
        gvCustomers.DataBind();
    }
}
 
private DataSet GetData(string query)
{
    string conString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
    SqlCommand cmd = new SqlCommand(query);
    using (SqlConnection con = new SqlConnection(conString))
    {
        using (SqlDataAdapter sda = new SqlDataAdapter())
        {
            cmd.Connection = con;
 
            sda.SelectCommand = cmd;
            using (DataSet ds = new DataSet())
            {
                sda.Fill(ds);
                return ds;
            }
        }
    }
}
 
VB.Net
Protected Sub Page_Load(ByVal sender As ObjectByVal e As EventArgs) Handles Me.Load
    If Not IsPostBack Then
        gvCustomers.DataSource = GetData("SELECT ContactName, Country FROM Customers")
        gvCustomers.DataBind()
    End If
End Sub
 
Private Function GetData(query As String) As DataSet
    Dim conString As String ConfigurationManager.ConnectionStrings("constr").ConnectionString
    Dim cmd As New SqlCommand(query)
    Using con As New SqlConnection(conString)
        Using sda As New SqlDataAdapter()
            cmd.Connection = con
 
            sda.SelectCommand = cmd
            Using ds As New DataSet()
                sda.Fill(ds)
                Return ds
            End Using
        End Using
    End Using
End Function
 
 

Binding the ASP.Net DropDownList in ItemTemplate

The RowDataBound event of the ASP.Net GridView is used to bind the ASP.Net DropDownList with the Countries form the Northwind database, once the DropDownList is populated, the Country to which the Customer belongs is set as the Selected in the DropDownList.
C#
protected void OnRowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        //Find the DropDownList in the Row
        DropDownList ddlCountries = (e.Row.FindControl("ddlCountries") as DropDownList);
        ddlCountries.DataSource GetData("SELECT DISTINCT Country FROM Customers");
        ddlCountries.DataTextField "Country";
        ddlCountries.DataValueField "Country";
        ddlCountries.DataBind();
 
        //Add Default Item in the DropDownList
        ddlCountries.Items.Insert(0, new ListItem("Please select"));
 
        // Select the Country of Customer in DropDownList
        string country = (e.Row.FindControl("lblCountry")as Label).Text;
        ddlCountries.Items.FindByValue(country).Selected = true;
    }
}
 
VB.Net
Protected Sub OnRowDataBound(ByVal sender As ObjectByVal e As GridViewRowEventArgs)
    If (e.Row.RowType DataControlRowType.DataRow) Then
        'Find the DropDownList in the Row
        Dim ddlCountries As DropDownList = CType(e.Row.FindControl("ddlCountries"), DropDownList)
        ddlCountries.DataSource GetData("SELECT DISTINCT Country FROM Customers")
        ddlCountries.DataTextField "Country"
        ddlCountries.DataValueField "Country"
        ddlCountries.DataBind()
        'Add Default Item in the DropDownList
        ddlCountries.Items.Insert(0, New ListItem("Please select"))
        ' Select the Country of Customer in DropDownList
        Dim country As String = CType(e.Row.FindControl("lblCountry"),Label).Text
        ddlCountries.Items.FindByValue(country).Selected =  True
    End If
End Sub
 
 

Screenshot

Bind DropDownList in ItemTemplate of TemplateField in ASP.Net GridView
 
 

Demo

 
 

Downloads