How to Bind Data in Dropdownlist in Gridview through C# coding in asp.net


Binding Data into Dropdownlist is pretty easy task to us. But how much easy or difficult to Bind Data into Dropdownlist when that Dropdownlist Control present in Gridview Controls. In these articles we see How to Bind Data in Dropdownlist in Gridview through C# coding in asp.net.

Let me show you how to add Dropdownlist Control to Gridview, and retrieve data from SQL Server and Bind that data into Gridview control and also Bind Data to that Dropdownlist control which available in Gridview Control. Below image show After binding data to gridview control.
Gridview DropdownList SelectedIndexChanged Event in Asp.net C#

Create Html Page


Add Gridview Control to design view to display sampledata in it.

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ExceltoGridview.aspx.cs" Inherits="ExceltoGridview" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div align="center">
<asp:GridView ID="Gridview1" AutoGenerateColumns="False"
HeaderStyle-CssClass="headerstyle" runat="server" BackColor="White"
BorderColor="White" BorderStyle="Ridge" BorderWidth="2px" CellPadding="3"
CellSpacing="1" GridLines="None" DataKeyNames="firstname, lastname"
onrowdatabound="GridView1_RowDataBound" >
<Columns>
<asp:TemplateField HeaderText="Firstname">
<ItemTemplate>
<asp:Label ID="lblfirstname" runat="server" Text='<%# ("firstname") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Lastname">
<ItemTemplate>
<asp:Label ID="lbllastname" runat="server" Text='<%# ("lastname") %>'/>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Date-Of-Birth">
<ItemTemplate>
<asp:Label ID="lbldob" runat="server" Text='<%# ("dob") %>'/>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Age">
<ItemTemplate>
<asp:Label ID="lblage" runat="server" Text='<%# ("age") %>'/>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Status">
<ItemTemplate>
<asp:Label ID="lblstatus" runat="server" Text='<%# ("status") %>' Visible = "false" />
<asp:DropDownList ID="ddl_status" Width="120px" runat="server" AutoPostBack="True" onselectedindexchanged="ddl_status_SelectedIndexChanged"></asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<FooterStyle BackColor="#C6C3C6" ForeColor="Black" />

<HeaderStyle CssClass="header_th"></HeaderStyle>
<PagerStyle BackColor="#C6C3C6" ForeColor="Black" HorizontalAlign="Right" />
<RowStyle BackColor="#DEDFDE" ForeColor="Black" CssClass="row_td" />
<SelectedRowStyle BackColor="#9471DE" Font-Bold="True" ForeColor="White" />
<SortedAscendingCellStyle BackColor="#F1F1F1" />
<SortedAscendingHeaderStyle BackColor="#594B9C" />
<SortedDescendingCellStyle BackColor="#CAC9C9" />
<SortedDescendingHeaderStyle BackColor="#33276A" />
</asp:GridView>
</div>
<div align="center">
</form>
</body>
</html>

Namespace :


Add namespace to code behind in csharp coding

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Configuration;
using System.Data.SqlClient;

C# Coding :



public partial class Pending : System.Web.UI.Page
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ms"].ConnectionString);
SqlDataAdapter da;
DataSet ds = new DataSet();
protected void Page_Load(object sender, EventArgs e)
{
try
{
//Retrieve sampledata and display in gridview
da = new SqlDataAdapter("select * from sampledata ", con);
con.Open();
da.Fill(ds);
if (ds.Tables[0].Rows.Count > 0)
{
GridView1.DataSource = ds;
GridView1.DataBind();
}
}
catch
{
con.Close();
}
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
try
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
da = new SqlDataAdapter("select * from sampledata", con);
//Find the DropDownList in the Row
DropDownList ddl_status = (e.Row.FindControl("ddl_status") as DropDownList);
//Select the Country of Customer in DropDownList
da.Fill(ds);
if (ds.Tables[0].Rows.Count > 0)
{
SqlDataAdapter da1 = new SqlDataAdapter("select statusid, statusname from statusdetails", con);
DataSet ds1 = new DataSet();
da1.Fill(ds1);
if (ds1.Tables[0].Rows.Count > 0)
{
ddl_status.DataSource = ds1;
ddl_status.DataTextField = "statusname";
ddl_status.DataBind();
string status = (e.Row.FindControl("lblstatus") as Label).Text;
ddl_status.Items.FindByValue(status).Selected = true;
//Add Default Item in the DropDownList
ddl_status.Items.Insert(0, new ListItem("Please select"));
}
}
}
}
catch
{
con.Close();
}
}

protected void ddl_status_SelectedIndexChanged(object sender, EventArgs e)
{
//if choose another value in dropdownlist its going to update database with selected values
try
{
DropDownList ddl_status = (DropDownList)sender;
GridViewRow row = (GridViewRow)ddl_status.Parent.Parent;
int idx = row.RowIndex;


//Retrieve firstname and lastam from Gridview and status(dropdownlist)
String lblfirstname = ((Label)row.Cells[0].FindControl("lblfirstname")).Text;
String lbllastname= ((Label)row.Cells[0].FindControl("lbllastname")).Text;
DropDownList ddl = (DropDownList)row.Cells[0].FindControl("ddl_status");


//Update Status
string query = "Update sampledata set status='" + ddl.Text.ToString() + "' where firstname='" + lblfirstname + "'";
SqlCommand cmd = new SqlCommand(query, con);
con.Open();
cmd.ExecuteNonQuery();
}
catch
{
con.Close();

}
}
}


Attachments

Comments



  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: