Tuesday, 1 April 2014

How to bind data to dropdown list in GridView RowDataBound event using ASP.NET


In this article I will explain how to bind data to dropdown list in GridView RowDataBound event using ASP.NET

In this scenario we used two SQL tables. One for Data bind to gridview and another one for Dropdown list data binding.

I have two tables in DataBase.

1.      City Table (tblCity)

2.      GridView Table (tblCascading)





First we need to establish a connection in Web.config file.

<connectionStrings>
<add name="connectionString" connectionString="Data Source=LocalHost;uid=sa1;password=Con@123;Initial Catalog=practice"/>
</connectionStrings>

Below is the page design.




<!DOCTYPE html> 
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<table align="center">
<tr>
<td style="color: maroon; font-size: large;">
<b><u>In Gridview Populate one dropdown List</u></b>
</td>
</tr>
<tr>
<td height="20px"></td>
</tr>
<tr>
<td>
<asp:GridView runat="server" ID="gvCascading" AutoGenerateColumns="false" ShowFooter="true" ShowHeader="true" OnRowDataBound="gvCascading_RowDataBound">
<Columns>
<asp:TemplateField HeaderText="City Name">
<ItemTemplate>
<asp:Label ID='lblCityName' runat="Server" Text='<%# Eval("CityName") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:DropDownList ID="ddlCity" runat="server" Width="150px"></asp:DropDownList>
</FooterTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</td>
</tr>
</table>
</form>
</body>
</html>

Below is the total code for bind data to dropdown list in GridView RowDataBound event using ASP.NET

using System;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Web.UI.WebControls;

public partial class GridviewRowdataboundevent : System.Web.UI.Page
{
SqlConnection conCascading = new SqlConnection(ConfigurationManager.ConnectionStrings["Connection"].ConnectionString);
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindGridView();
}
}
protected void BindGridView()
{
conCascading.Open();
SqlCommand cmdCountry = new SqlCommand("select * from tblCascading", conCascading);
SqlDataAdapter daCountry = new SqlDataAdapter(cmdCountry);
DataSet dsCountry = new DataSet();
daCountry.Fill(dsCountry);
conCascading.Close();
gvCascading.DataSource = dsCountry;
gvCascading.DataBind();
}
protected void gvCascading_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Footer)
{
DropDownList ddlCity = (DropDownList)e.Row.FindControl("ddlCity");
conCascading.Open();
SqlCommand cmdCountry = new SqlCommand("select * from tblCity", conCascading);
SqlDataAdapter daCity = new SqlDataAdapter(cmdCountry);
DataSet dsCity = new DataSet();
daCity.Fill(dsCity);
conCascading.Close();
ddlCity.DataSource = dsCity;
ddlCity.DataTextField = "CityName";
ddlCity.DataValueField = "CityID";
ddlCity.DataBind();
ddlCity.Items.Insert(0, new ListItem("--Select City--", "0"));

}
}
}

Output:



No comments:

Post a Comment