Tuesday, 8 March 2016

How to bind DropDownList control in GridView control in asp.net

In this article, we are going to learn how to bind DropDownList control in GridView control in asp.net. We will learn binding DropDownList while editing GridView control in EditItemTemplate and while page loads in FooterTemplate.

Related Articles

  1. How to bind DropDownList in asp.net using dummy DataTable
  2. How to implement cascaded DropDownList in asp.net
  3. How to bind DropDownList from DataBase in ASP.NET with C#
  4. Bind DropDownList in EditItemTemplate of GridView with selected value using Button edit CommandName in asp.net

In Designer File

In designer file, create a GridView control named as grdStudent. Generate OnRowEditing and OnRowCancelingEdit events.

<div align="center">
<asp:GridView ID="grdStudent" runat="server" AutoGenerateColumns="False"
AllowPaging="true" ShowFooter="true" PageSize="5"
OnRowCancelingEdit="grdStudent_RowCancelingEdit" OnRowEditing="grdStudent_RowEditing"
BorderColor="SkyBlue">
<HeaderStyle BackColor="#00A6DC" ForeColor="White" />
<Columns>
    <asp:TemplateField HeaderText="Student Id" Visible="false">
        <ItemTemplate>
            <asp:Label ID="lblStudentId" runat="server" Text='<%#Eval("StudentId") %>'>
            </asp:Label>
        </ItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField HeaderText="Student Name">
        <ItemTemplate>
            <asp:Label ID="lblStudentName" runat="server" Text='<%#Eval("StudentName") %>'>
            </asp:Label>
        </ItemTemplate>
        <EditItemTemplate>
            <asp:TextBox ID="eStudentName" runat="server" Text='<%#Eval("StudentName") %>'>
            </asp:TextBox>
        </EditItemTemplate>
        <FooterTemplate>
            <asp:TextBox ID="ftxtStudentName" runat="server"></asp:TextBox>
        </FooterTemplate>
    </asp:TemplateField>
    <asp:TemplateField HeaderText="Student Subject">
        <ItemTemplate>
            <asp:Label ID="lblStudentSubject" runat="server" Text='<%#Eval("StudentSubject") %>'>
            </asp:Label>
        </ItemTemplate>
        <EditItemTemplate>
            <asp:DropDownList ID="eStudentSubject" runat="server"></asp:DropDownList>
        </EditItemTemplate>
        <FooterTemplate>
            <asp:DropDownList ID="ftxtStudentSubject" runat="server"></asp:DropDownList>
        </FooterTemplate>
    </asp:TemplateField>
    <asp:TemplateField HeaderText="Operations">
        <ItemTemplate>
            <asp:Button ID="btnEdit" Text="Edit" runat="server" CommandName="Edit" />
        </ItemTemplate>
        <EditItemTemplate>
            <asp:Button ID="btnCancel" Text="Cancel" runat="server" CommandName="Cancel" />
        </EditItemTemplate>
        <FooterTemplate>
            <asp:Button ID="fbtnAdd" runat="server" Text="Add" OnClick="fbtnAdd_Click" />
        </FooterTemplate>
    </asp:TemplateField>
</Columns>
</asp:GridView>
</div>

Namespace Used

Include the below Namespace.

using System.Data;

Output

When page loads, I am binding gridview with some dummy data from C-sharp code.

page load bind dropdown in gridview in asp.net

When you click on add button of footer,the fbtnAdd_Click() event will get call and student subjects will get bind in footer row.

bind dropdown in gridview on add button click in asp.net

When you click on edit button,the grdStudent_RowEditing() event will get call and student subjects wil get bind on respective row.

bind dropdown in gridview on edit button click in asp.net

Complete C# Code

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindStudentGrid();
            }
        }

        private void BindStudentGrid()
        {  //Bind Student gridview
            grdStudent.DataSource = GetStudentDetails();
            grdStudent.DataBind();
        }

        private DataTable GetStudentDetails()
        {
            //Creating DataTable of Student
            DataTable dt = new DataTable();
            //adding Columns to DataTable
            dt.Columns.Add("StudentId", typeof(Int32));
            dt.Columns.Add("StudentName", typeof(string));
            dt.Columns.Add("StudentSubject", typeof(string));
            //adding Rows to DataTable
            dt.Rows.Add(1, "Rahul", "Maths");
            dt.Rows.Add(2, "Sachin", "Physics");
            return dt;
        }

        private DataTable GetSubjects()
        {
            //Creating DataTable for List of Subjects
            DataTable dt = new DataTable();
            //adding Column to DataTable
            dt.Columns.Add("StudentSubject", typeof(string));
            //adding Rows to DataTable
            dt.Rows.Add("--Select--");
            dt.Rows.Add("Maths");
            dt.Rows.Add("Physics");
            dt.Rows.Add("Chemistry");
            dt.Rows.Add("History");
            dt.Rows.Add("Geography");
            return dt;
        }

        protected void fbtnAdd_Click(object sender, EventArgs e)
        {
            //find the subject DropDownList of footertemplate
            DropDownList ftxtStudentSubject = (DropDownList)grdStudent.FooterRow.FindControl(
            "ftxtStudentSubject");
            // assigning the subject datatable to dropdownlist
            ftxtStudentSubject.DataSource = GetSubjects();
            ftxtStudentSubject.DataTextField = "StudentSubject";
            ftxtStudentSubject.DataValueField = "StudentSubject";
            ftxtStudentSubject.DataBind();
        }

        protected void grdStudent_RowEditing(object sender, GridViewEditEventArgs e)
        {
            // set the GridView to edit mode
            grdStudent.EditIndex = e.NewEditIndex;
            BindStudentGrid();
            //find the subject DropDownList of EditItemTemplate
            DropDownList eStudentSubject = (DropDownList)grdStudent.Rows[grdStudent.EditIndex].FindControl(
            "eStudentSubject");
            // assigning the subject DataTable to DropDownList
            eStudentSubject.DataSource = GetSubjects();
            eStudentSubject.DataTextField = "StudentSubject";
            eStudentSubject.DataValueField = "StudentSubject";
            eStudentSubject.DataBind();
        }

        protected void grdStudent_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            //set GridView in initial mode
            grdStudent.EditIndex = -1;
            BindStudentGrid();
        }
        
Share:

0 comments:

Post a Comment

Email Subscription

Subscribe to our newsletter to get the latest articles directly into your inbox

Blog Archive

BUY FROM AMAZON