Dropdownlist in asp.net grid view

How to Bind DropdownList in Grid Template Field:



We can define dropdownlist in grid by using template field. It is very simple to take controls with in asp.net grid. Dropdown list control is use to display data as list and provide facility to user that he/she select only one item at a time . And Grid is use to display data in table format. In this Post we trying to get and bind dropdown list in to grid according to the unique key concept.
In this Asp.net programming tutorials Blog we give many Examples to refer to the dropdown list in asp.net and Asp.net gridview control. Know in this post we trying to combine these control and create an Example.


Dropdownlist Related Post:


How to get dropdownlist in grid Template:


<asp:TemplateField HeaderText="Select Cite" ItemStyle-HorizontalAlign="Center">
   <ItemTemplate>
    <asp:DropDownList ID="DropDownList1" runat="server">
     </asp:DropDownList>                             
  </ItemTemplate>
<ItemStyle HorizontalAlign="Center"></ItemStyle>
</asp:TemplateField>
DropDown in Gridview asp.net
DropDown in Gridview asp.net

Dropdownlist Related Post:


Gridview Related Post:

Code of .aspx page for Example of dropdownlist in grid:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Exampleoftemplate.aspx.cs" Inherits="Exampleoftemplate" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false"
            Height="258px" Width="559px" onrowdatabound="GridView1_RowDataBound"
            onselectedindexchanged="GridView1_SelectedIndexChanged">
        <Columns>
         <asp:TemplateField HeaderText="SNo" ItemStyle-HorizontalAlign="Center">
                            <ItemTemplate>
                                <%#Container.DataItemIndex+1 %>
                            </ItemTemplate>
                            <ItemStyle HorizontalAlign="Center"></ItemStyle>
                        </asp:TemplateField>
         <asp:TemplateField HeaderText="State ID" ItemStyle-HorizontalAlign="Center">
                            <ItemTemplate>
                                <asp:Label ID="lblStateID" runat="server" Text='<%# Eval("StateID") %>'></asp:Label>
                               
                            </ItemTemplate>
                            <ItemStyle HorizontalAlign="Center"></ItemStyle>
                        </asp:TemplateField>
         <asp:TemplateField HeaderText="Name of State" ItemStyle-HorizontalAlign="Center">
                            <ItemTemplate>
                               
                                <asp:Label ID="lblName" runat="server" Text='<%# Eval("StateName") %>'></asp:Label>
                            </ItemTemplate>
                            <ItemStyle HorizontalAlign="Center"></ItemStyle>
                        </asp:TemplateField>
                       <asp:TemplateField HeaderText="Select Cite" ItemStyle-HorizontalAlign="Center">
                            <ItemTemplate>
                                <asp:DropDownList ID="DropDownList1" runat="server">
                                </asp:DropDownList>                             
                            </ItemTemplate>
                            <ItemStyle HorizontalAlign="Center"></ItemStyle>
                        </asp:TemplateField>

        </Columns>
            <HeaderStyle BackColor="#3333FF" BorderColor="#FF99FF" BorderStyle="Groove"
                BorderWidth="2px" ForeColor="White" />
        </asp:GridView>
    </div>
    </form>
</body>
</html>


Video for Getting Dropdown list in Gridview:




Code of .aspx page for Example of dropdownlist in grid:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;

public partial class Exampleoftemplate : System.Web.UI.Page
{
    string _connectionstring;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            _connectionstring = @"Data Source=PARIJAT-PC\PARIJAT;Initial Catalog=Database1;Integrated Security=True;Pooling=False";// create connection....
            BindData();// make function .......and call here........
        }
    }

    private void BindData()
    {
        try
        {

            string _sql = "select StateID,StateName from States";//sql Query
            SqlConnection _connection = new SqlConnection(_connectionstring);
            SqlCommand _command = new SqlCommand(_sql, _connection);
            SqlDataAdapter _adapter = new SqlDataAdapter(_command);
            DataTable datatable = new DataTable();
            _adapter.Fill(datatable);

            GridView1.DataSource = datatable;  //bind grid .........here
            GridView1.DataBind();

        }
        catch
        {
            throw;
        }
    }
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            try
            {
                //find control which are define in to grid ..................
                DataRow row = ((DataRowView)e.Row.DataItem).Row;
                Label lblStateID = (Label)e.Row.FindControl("lblStateID");
                DropDownList DropDownList1 = (DropDownList)e.Row.FindControl("DropDownList1");
                //find dropdown..................
                string _sql = "SELECT CityID,CityName FROM City WHERE StateID = '" + lblStateID.Text + "'";
                SqlConnection _connection = new SqlConnection(_connectionstring);
                SqlCommand _command = new SqlCommand(_sql, _connection);
                SqlDataAdapter _adapter = new SqlDataAdapter(_command);
                DataTable datatable1 = new DataTable();
                _adapter.Fill(datatable1);
                DropDownList1.DataSource = datatable1;
                DropDownList1.DataTextField = "CityName";
                DropDownList1.DataValueField = "CityName";
                DropDownList1.DataBind();
            }
            catch (Exception ex)
            {

              
            }
        }
    }
    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {

    }

}
Bind_Dropdown_intemplatefield
Bind_Dropdown_in template field


Comments