Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

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.

In designer file:

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


div align="center">
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" />

TemplateField HeaderText="Student Id" Visible="false">

Label ID="lblStudentId" runat="server" Text=''>
Label>

TemplateField>
TemplateField HeaderText="Student Name">

Label ID="lblStudentName" runat="server" Text=''>
Label>


TextBox ID="eStudentName" runat="server" Text=''>
TextBox>


TextBox ID="ftxtStudentName" runat="server">TextBox>

TemplateField>
TemplateField HeaderText="Student Subject">

Label ID="lblStudentSubject" runat="server" Text=''>
Label>


DropDownList ID="eStudentSubject" runat="server">DropDownList>


DropDownList ID="ftxtStudentSubject" runat="server">DropDownList>

TemplateField>
TemplateField HeaderText="Operations">

Button ID="btnEdit" Text="Edit" runat="server" CommandName="Edit" />


Button ID="btnCancel" Text="Cancel" runat="server" CommandName="Cancel" />


Button ID="fbtnAdd" runat="server" Text="Add" OnClick="fbtnAdd_Click" />

TemplateField>

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.


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



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



Complete 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();
}


This post first appeared on ASPArticles, please read the originial post: here

Share the post

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

×

Subscribe to Asparticles

Get updates delivered right to your inbox!

Thank you for your subscription

×