Friday, May 25, 2012

BInd data to dropdownlist in gridview in

Title: How to bind data to drop down list in using

In previous posts i explained bind data to grid view,bind data to dropdownlist,Bind drop down in MVC4.Bind grid view using LINQ. Here i will show how to binding the data to Drop down List inside of Grid View.Here the dropdownlist has place inside of grid view template Field .You can observe here i will bind data to both drop down and grid view While loading page.

<html xmlns="" >
<title>Bind data to Dropdownlist in gridview in</title>
<form id="form1" runat="server">
<asp:GridView ID="GvSalesData" AllowSorting="True" Runat="server"
AllowPaging="true" AutoGenerateColumns="False" >
<asp:TemplateField HeaderText="SalesID">
 <%#Eval("SID")  %>
<asp:TemplateField HeaderText="SalesPName">
<%#Eval("SalesPersonName")  %>
 <asp:TemplateField HeaderText="Samount">
<%#Eval("Samt")  %>
<asp:TemplateField HeaderText="SalesCity">
<asp:DropDownList ID="ddl_city" runat="server">
Code behind:
using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Data.SqlClient;

public partial class Dropdowningridview : System.Web.UI.Page
SqlConnection cn = new SqlConnection(ConfigurationManager.ConnectionStrings["bhaskarconnection"].Tostring());

protected void Page_Load(object sender, EventArgs e)
//Bind data to gridview
//Bind data to Dropdown in gridview
private void BindOrdrGrid()
 SqlDataAdapter Ordr_ad = new SqlDataAdapter("select * from Order_details", cn);
 DataSet Ordr_ds = new DataSet();
 Ordr_adp.Fill(Ordr_ds, "Order_details");
 GvSalesData.DataSource = Ordr_ds;
private void BindDropdownGrid()
SqlDataAdapter Ddl_ad = new SqlDataAdapter("select * from order_city", cn);
DataSet Ddl_ds = new DataSet();
ad.Fill(Ddl_ds, "order_city");
foreach (GridViewRow row in GvSalesData.Rows)
DropDownList _ddlcity = (DropDownList)(GvSalesData.Rows[row.RowIndex].Cells[4].FindControl("ddl_city"));
_ddlcity.DataSource = Ddl_ds;
_ddlcity.DataValueField = "CID";
_ddlcity.DataTextField = "City_name";
Post a Comment