How to add dropdownlist in gridview in asp.net c#

Many times we need to add dropdownlist in gridview  so in this article we learn about how to add dropdownlist gridview in asp.net c#.
Source Code:-
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="gridviewwithdropdown.aspx.cs"
    Inherits="gridviewwithdropdown" %>


<!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" RowStyle-HorizontalAlign="Center">
            <Columns>
            <asp:BoundField HeaderText="Serial No" DataField="Sno" />
            <asp:BoundField HeaderText="Name" DataField="Name" />
           
                <asp:TemplateField HeaderText="Course">
                    <ItemTemplate>
                        <asp:DropDownList ID="DropDownList1" runat="server" AppendDataBoundItems="true">
                            <asp:ListItem>MBA</asp:ListItem>
                            <asp:ListItem>B.tech</asp:ListItem>
                            <asp:ListItem>M.tech</asp:ListItem>
                        </asp:DropDownList>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:BoundField HeaderText="Contact Number" DataField="Contact" />
            </Columns>
        </asp:GridView>

    </div>
    </form>
</body>
</html>

Code behind(c#):-
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class gridviewwithdropdown : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        DataTable dtStudents = new DataTable();
        dtStudents.Columns.Add("Sno", typeof(string));
        dtStudents.Columns.Add("Name", typeof(string));
        dtStudents.Columns.Add("Contact", typeof(string));
        dtStudents.Rows.Add("1", "Neeraj Srivastava", "9450216123");
        dtStudents.Rows.Add("2", "Amit Saxena", "8560114924");
        dtStudents.Rows.Add("3", "Dheeraj Srivastava", "9450216111");

        dtStudents.Rows.Add("4", "Ankita Srivastava", "9450216560");
        dtStudents.Rows.Add("5", "Ankit Saxena", "9450216120");
        dtStudents.Rows.Add("6", "Ankur Saxena", "9044815455");
        dtStudents.Rows.Add("7", "Deepa Saxena", "90448154682");
        GridView1.DataSource = dtStudents;
        GridView1.DataBind();
    }
}

Out-Put:-


How to add dropdownlist in gridview in asp.net c# How to add dropdownlist in gridview in asp.net c# Reviewed by NEERAJ SRIVASTAVA on 4:53:00 PM Rating: 5

3 comments:

Powered by Blogger.