A Drop Down Inside a Grid View
Drop Down Inside a GridView
Thanks :Mudassar Ahmed Khan http://www.aspsnippets.com/Articles/How-to-populate-DropDownList-in-GridView-in-ASPNet.aspx
aspx page :
<%@ Page Title="Home Page" Language="C#" CodeBehind="Default.aspx.cs" Inherits="Indexers2._Default" %>
<form id="form1" runat="server">
<asp:GridView ID = "grdView"
runat = "server"
CellPadding = "5"
AutoGenerateColumns="false"
>
<Columns>
<asp:BoundField HeaderText="Name" DataField="CONTACTNAME" />
<asp:TemplateField HeaderText="Country">
<ItemTemplate>
<asp:DropDownList ID = "drpdwnlst"
runat = "server"
>
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</form>
<form id="form1" runat="server">
<asp:GridView ID = "grdView"
runat = "server"
CellPadding = "5"
AutoGenerateColumns="false"
>
<Columns>
<asp:BoundField HeaderText="Name" DataField="CONTACTNAME" />
<asp:TemplateField HeaderText="Country">
<ItemTemplate>
<asp:DropDownList ID = "drpdwnlst"
runat = "server"
>
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</form>
C# Code Behined
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace Indexers2
{ public partial class _Default : Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
//EVENT DELEGAT//
grdView.RowDataBound += grdView_RowDataBound;
grdView.DataSource = GetData("SELECT CONTACTNAME,COUNTRY FROM people");
grdView.DataBind();
}
}
void grdView_RowDataBound(object sender, GridViewRowEventArgs e)
{
if(e.Row.RowType==DataControlRowType.DataRow)
{
/* DROPDOWNLIST BEING POPULATED FROM THE DATABASE .EVEN IF THE DROP DOWN IS INSIDE A GRID VIEW*/
/* ON ROW DATA BOUND ONLY WE HAVE TO ACCESS THE VALUE USING GRIDVIEWROWEVENTARS OBJECT*/
DropDownList drpcountries=(e.Row.FindControl("drpdwnlst") as DropDownList);
drpcountries.DataSource=GetData("SELECT DISTINCT COUNTRY from people");
drpcountries.DataTextField="Country";
drpcountries.DataValueField="Country";
drpcountries.DataBind();
drpcountries.Items.Insert(0,new ListItem("Please Select"));
/* CODE TO SHOW WHEN VALUE FROM DATABASE NEEDS TO BE SHOWED IN THE DROPDOWN LIST*/
// string country=(e.Row.FindControl("Country") as Label).Text;
// drpcountries.Items.FindByValue(country).Selected=true;
}
}
public DataSet GetData(String sqlstmt)
{
string constring = ConfigurationManager.ConnectionStrings["ConnectionStringTest"].ConnectionString;
SqlCommand cmd= new SqlCommand(sqlstmt);
using (SqlConnection conn=new SqlConnection(constring))
{
using(SqlDataAdapter sda=new SqlDataAdapter())
{
cmd.Connection=conn;
sda.SelectCommand=cmd;
using(DataSet ds= new DataSet())
{
sda.Fill(ds);
return ds;
}
}
}
}
}
}
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace Indexers2
{ public partial class _Default : Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
//EVENT DELEGAT//
grdView.RowDataBound += grdView_RowDataBound;
grdView.DataSource = GetData("SELECT CONTACTNAME,COUNTRY FROM people");
grdView.DataBind();
}
}
void grdView_RowDataBound(object sender, GridViewRowEventArgs e)
{
if(e.Row.RowType==DataControlRowType.DataRow)
{
/* DROPDOWNLIST BEING POPULATED FROM THE DATABASE .EVEN IF THE DROP DOWN IS INSIDE A GRID VIEW*/
/* ON ROW DATA BOUND ONLY WE HAVE TO ACCESS THE VALUE USING GRIDVIEWROWEVENTARS OBJECT*/
DropDownList drpcountries=(e.Row.FindControl("drpdwnlst") as DropDownList);
drpcountries.DataSource=GetData("SELECT DISTINCT COUNTRY from people");
drpcountries.DataTextField="Country";
drpcountries.DataValueField="Country";
drpcountries.DataBind();
drpcountries.Items.Insert(0,new ListItem("Please Select"));
/* CODE TO SHOW WHEN VALUE FROM DATABASE NEEDS TO BE SHOWED IN THE DROPDOWN LIST*/
// string country=(e.Row.FindControl("Country") as Label).Text;
// drpcountries.Items.FindByValue(country).Selected=true;
}
}
public DataSet GetData(String sqlstmt)
{
string constring = ConfigurationManager.ConnectionStrings["ConnectionStringTest"].ConnectionString;
SqlCommand cmd= new SqlCommand(sqlstmt);
using (SqlConnection conn=new SqlConnection(constring))
{
using(SqlDataAdapter sda=new SqlDataAdapter())
{
cmd.Connection=conn;
sda.SelectCommand=cmd;
using(DataSet ds= new DataSet())
{
sda.Fill(ds);
return ds;
}
}
}
}
}
}
DataBase Script
create table people
(
CONTACTNAME varchar(30),
COUNTRY varchar(30)
)
(
CONTACTNAME varchar(30),
COUNTRY varchar(30)
)
INSERT INTO people values('AMIT','INDIA');
INSERT INTO people values('AJAY','INDIA');
INSERT INTO people values('AJIT','INDIA');
INSERT INTO people values('AJIT_K','INDONESIA');
INSERT INTO people values('AJIT_M','BURMA');
INSERT INTO people values('AJAY','INDIA');
INSERT INTO people values('AJIT','INDIA');
INSERT INTO people values('AJIT_K','INDONESIA');
INSERT INTO people values('AJIT_M','BURMA');
WebConfig file
<connectionStrings>
<add name="ConnectionStringTest" providerName="Syste,.Data.SqlClient" connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename=H:\MvcApplication1\MvcApplication1\MvcApplication1\App_Data\Models_-20150625114416.mdf;Integrated Security=True;Connect Timeout=30"/>
</connectionStrings>
<add name="ConnectionStringTest" providerName="Syste,.Data.SqlClient" connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename=H:\MvcApplication1\MvcApplication1\MvcApplication1\App_Data\Models_-20150625114416.mdf;Integrated Security=True;Connect Timeout=30"/>
</connectionStrings>
Comments
Post a Comment