Introdcution:-
In this article , I have explained how to bind
dropdownlist in gridview footer template (footer row) in asp.net using c#. For
Binding a Footer template (footer row) ,first you have to find dropdown in
gridview using FindControl.
DataBase:-
Create table Customers(ContactName varchar(max),City
varchar(200),Country varchar(50));
Insert some dummy data in this table.
Design page:-
Drag and drop GridView Control on design page and
customize the gridview like below.
<asp:GridView ID="GridView1"
runat="server"
AutoGenerateColumns="false"
ShowFooter="true">
<Columns>
<asp:TemplateField>
<FooterTemplate>
<asp:Button ID="btnAdd" runat="server" Text="Add" onclick="btnAdd_Click" />
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Name">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%#Eval("ContactName")
%>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtname" runat="server" Width="150px"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="City">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%#Eval("City")
%>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:DropDownList ID="ddcity" runat="server" Width="150px">
</asp:DropDownList>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Country">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%#Eval("Country")
%>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:DropDownList ID="ddcountry" runat="server" Width="150px">
</asp:DropDownList>
</FooterTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Code Behind Page:-
using System;
using
System.Collections;
using
System.Configuration;
using
System.Data;
using
System.Linq;
using
System.Web;
using
System.Web.Security;
using
System.Web.UI;
using
System.Web.UI.HtmlControls;
using
System.Web.UI.WebControls;
using
System.Web.UI.WebControls.WebParts;
using
System.Xml.Linq;
using
System.Data.SqlClient;
public partial class BindGridViewFooterColumn : System.Web.UI.Page
{
string
constring = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
SqlConnection
con;
SqlCommand
cmd;
protected void Page_Load(object
sender, EventArgs e)
{
if (!this.IsPostBack)
{
GridBind();
BindCountry();
BindCity();
}
}
public void GridBind()
{
GridView1.DataSource = GetData("Select ContactName,City,Country from
Customers");
GridView1.DataBind();
}
public void BindCountry()
{
DropDownList
ddcountry =(DropDownList)GridView1.FooterRow.FindControl("ddcountry");
ddcountry.DataSource = GetData("Select DISTINCT Country from Customers");
ddcountry.DataTextField = "Country";
ddcountry.DataValueField = "Country";
ddcountry.DataBind();
ddcountry.Items.Insert(0, new ListItem("Select", "0"));
}
public void BindCity()
{
DropDownList
ddcity = (DropDownList)GridView1.FooterRow.FindControl("ddcity");
ddcity.DataSource = GetData("Select DISTINCT City from Customers");
ddcity.DataTextField = "City";
ddcity.DataValueField = "City";
ddcity.DataBind();
ddcity.Items.Insert(0, new ListItem("Select", "0"));
}
public DataTable GetData(string
query)
{
using
(con = new SqlConnection(constring))
{
using
(cmd = new SqlCommand())
{
cmd.CommandText = query;
using
(SqlDataAdapter da = new SqlDataAdapter())
{
cmd.Connection = con;
da.SelectCommand = cmd;
using
(DataTable dt = new
DataTable())
{
da.Fill(dt);
return dt;
}
}
}
}
}
protected void btnAdd_Click(object
sender, EventArgs e)
{
TextBox name=(TextBox)GridView1.FooterRow.FindControl("txtname");
DropDownList
city = (DropDownList)GridView1.FooterRow.FindControl("ddcity");
DropDownList
country = (DropDownList)GridView1.FooterRow.FindControl("ddcountry");
con = new SqlConnection(constring);
con.Open();
cmd = new
SqlCommand("Insert
into Customers(ContactName,City,Country) values('"+name.Text+"','"+city.Text+"','"+country.Text+"')", con);
cmd.ExecuteNonQuery();
con.Close();
GridBind();
}
}
No comments:
Post a Comment
Note: only a member of this blog may post a comment.