Title: How to bind data to drop down list in asp.net using c#.net
Description:
In previous posts i explained Asp.net 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.
Description:
In previous posts i explained Asp.net 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="http://www.w3.org/1999/xhtml" > <head> <title>Bind data to Dropdownlist in gridview in asp.net</title> </head> <body> <form id="form1" runat="server"> <div> <asp:GridView ID="GvSalesData" AllowSorting="True" Runat="server" AllowPaging="true" AutoGenerateColumns="False" > <Columns> <asp:TemplateField HeaderText="SalesID"> <ItemTemplate> <%#Eval("SID") %> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="SalesPName"> <ItemTemplate> <%#Eval("SalesPersonName") %> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Samount"> <ItemTemplate> <%#Eval("Samt") %> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="SalesCity"> <ItemTemplate> <asp:DropDownList ID="ddl_city" runat="server"> </asp:DropDownList> </ItemTemplate> </asp:TemplateField> </Columns> </asp:GridView> </div> </form> </body> </html>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) { If(!page.IsPostBack) { //Bind data to gridview BindOrdrGrid(); //Bind data to Dropdown in gridview BindDropdownGrid(); } 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; GvSalesData.DataBind(); } private void BindDropdownGrid() { cn.Open(); 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"; _ddlcity.DataBind(); } }
3 comments:
Thanks bhaskar,It helps a lot.
Thanks for your comment.
It works well for me.But the alignment is not fit for your code.
Post a Comment