* IBM to offer mobile security as a service IBM will start delivering mobile security-as-a-service (MaaS) through its own cloud infrastructure in India, a move that is expected to better address in-country data requirements... IBM Spectrum Computing: IBM has enlarged its portfolio of software-defined infrastructure solutions with cognitive features for workload management.  * LiFi internet technology has been introduced, the new type of wireless internet connection that gives 100 times faster than traditional WiFi -- Invented by Professor Harald Haas from the University of Edinburgh. * Sci. Rachid Yazami has developed a smart chip that charges smartphones in less than 10 minutes. BenQ has launched BlueCore projector - Consumer electronics major BenQ has launched its first BlueCore laser light source projector. For those unaware, devices with BlueCore laser technology have a high contrast output of 80000:1 with an extended lamp life and efficiency. *** 
  Jan 25 2015 9:41AM     Michael Ryan
  0 Comments    10160 Views  
Here Michael Ryan provided a short tutorial with example, how to bind Gridview with DropDownList in ASP.Net using Dataset in C#/VB
For binding the gridview, First have to create a table in Microsoft SQL Server Database as shown below.
Creating a table
When creating a table make Identity Specification as true for column 'ID'.

Set the table name as shown below.
Adding a GridView to the Page
Firstly you need to add a GridView control to the ASP.Net Web Page from the Visual Studio ToolBox as shown below.

Below is the sample HTML code,
<html xmlns="">
<head runat="server">
    <form id="form1" runat="server">
            Employee Details</h2>
        How to populate DropDownList in GridView in ASP.Net
        <br >
        <asp:GridView ID="GridEmployee" runat="server" AutoGenerateColumns="false" OnRowDataBound="OnRowDataBound"
         CellPadding="5" Style="background-color: rgb(241, 241, 241); width: 50%;">


                <asp:TemplateField HeaderText="S.No">
                        <%#Container.DataItemIndex+1 %>

                <asp:BoundField HeaderText="Name" DataField="EmployeeName" />
                <asp:BoundField HeaderText="Age" DataField="EmployeeAge" />
                <asp:TemplateField HeaderText="Designation">
                        <asp:Label ID="lblEmployeeDesc" runat="server" Text='<%# Eval("EmployeeDesc"%>'
                            Visible="false" />

                        <asp:DropDownList ID="ddlEmployeeDesc" runat="server" Width="150px">
                <asp:BoundField HeaderText="Experience" DataField="EmployeeExperience" />
 <HeaderStyle BackColor="#81B441" Font-Bold="true" ForeColor="White" />
You will need to import the following namespace.

using System.Data.SqlClient;
using System.Data;


Imports System.Data.SqlClient
Imports System.Data

Binding Gridview with Dropdownlist from Database using DataSet
Below is the sample code for binding gridview with filling DropDownList on RowDataBound from Database in C#/VB.

        protected void Page_Load(object sender, EventArgs e)
            if (!IsPostBack)
                GridEmployee.DataSource = GetData("SELECT EmployeeName, EmployeeAge, EmployeeDesc, EmployeeExperience FROM Employee");

        private DataSet GetData(string query)
            string conString = ConfigurationManager.ConnectionStrings["ConnectionString"].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())
                        return ds;

        protected void OnRowDataBound(object sender, GridViewRowEventArgs e)
            if (e.Row.RowType == DataControlRowType.DataRow)
                //Find the DropDownList in the Row
                DropDownList ddlCountries = (e.Row.FindControl("ddlEmployeeDesc"as DropDownList);
                ddlCountries.DataSource = GetData("SELECT DISTINCT EmployeeDesc FROM Employee");
                ddlCountries.DataTextField = "EmployeeDesc";
                ddlCountries.DataValueField = "EmployeeDesc";

                //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("lblEmployeeDesc"as Label).Text;
                ddlCountries.Items.FindByValue(country).Selected = true;


Protected Sub Page_Load(ByVal sender As ObjectByVal e As EventArgs)

        If Not IsPostBack Then
            GridEmployee.DataSource = GetData("SELECT EmployeeName, EmployeeAge, EmployeeDesc, EmployeeExperience FROM Employee")
        End If

    End Sub
    Private Function GetData(ByVal query As StringAs DataSet

        Dim conString As String = ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString
        Dim cmd  SqlCommand = New (query)
        Dim con  SqlConnection = New (conString)
        Dim sda  SqlDataAdapter = New ()
        cmd.Connection = con
        sda.SelectCommand = cmd
        Dim ds  DataSet = New Dataset() 
        Return ds

    End Function
    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("ddlEmployeeDesc"),DropDownList)
            ddlCountries.DataSource = GetData("SELECT DISTINCT EmployeeDesc FROM Employee")
            ddlCountries.DataTextField = "EmployeeDesc"
            ddlCountries.DataValueField = "EmployeeDesc"
            '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("lblEmployeeDesc"),Label).Text
            ddlCountries.Items.FindByValue(country).Selected = true
        End If

    End Sub

I hope this page will helps to create GridView with DropDownList in ASP.Net application. Thanks.

Recent Posts
Create Amazon ElasticCache Using Memcached in CSharp
Oct 29 2018 12:09PM Posted By Amose
Get Google Map Lat Lng (Geo Point) By Pincode in C#
Oct 28 2018 12:09PM Posted By Pranav
Google URL Shortener in C#
Oct 20 2018 12:09PM Posted By Sanjay
Bind Gridview from CSV file in Asp.Net C#
Oct 5 2018 12:09PM Posted By Michael
Call WebService method from jQuery in every 1 minute
Sep 26 2018 12:09PM Posted By John
Gridview custom CSS in ASP.Net
Sep 14 2018 12:09PM Posted By Micheal Ryan
Read excel file and bind to Gridview in C#
Sep 10 2018 12:09PM Posted By Micheal
Follow us on Facebook
Follow us on Google +
Recent post in your Email inbox.
Enter your email address: