Introduction
This article demonstrates one of interesting
and most useful concept in asp.net gridview control
Question: What is bind gridView from 1 page to another with session?
In simple terms "It enables to bind gridview data of one page to another page
gridview with session variable".
Step 1: Create a web application

Step 2: The complete code of WebForm1.aspx looks as below
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="BindGridViewswithSessionApp.WebForm1" %>
<!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 id="Head1" runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<center>
<div>
<table>
<tr>
<td colspan="2" align="center">
<asp:Label ID="Label1" runat="server" Text="Send Data From 1 Grid to Another Grid of Next Page"
Font-Bold="true" Font-Size="Large" Font-Names="Verdana" ForeColor="Maroon"></asp:Label>
</td>
</tr>
<tr>
<td colspan="2" align="center">
<asp:Button ID="Button1" runat="server" Text="Pass Data to Second Page" Font-Names="Verdana"
Width="213px" BackColor="Orange" Font-Bold="True" OnClick="Button1_Click" Style="height: 26px" />
</td>
</tr>
<tr>
<td colspan="2" align="center">
<asp:GridView ID="GridView1" runat="server" CssClass="grid" BackColor="LightGoldenrodYellow"
BorderColor="Tan" BorderWidth="1px" CellPadding="2" ForeColor="Black" GridLines="None"
AutoGenerateColumns="false">
<AlternatingRowStyle BackColor="PaleGoldenrod" />
<FooterStyle BackColor="Tan" />
<HeaderStyle BackColor="Tan" Font-Bold="True" />
<PagerStyle BackColor="PaleGoldenrod" ForeColor="DarkSlateBlue" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="DarkSlateBlue" ForeColor="GhostWhite" />
<SortedAscendingCellStyle BackColor="#FAFAE7" />
<SortedAscendingHeaderStyle BackColor="#DAC09E" />
<SortedDescendingCellStyle BackColor="#E1DB9C" />
<SortedDescendingHeaderStyle BackColor="#C2A47B" />
<Columns>
<asp:TemplateField HeaderText="FirstName">
<ItemTemplate>
<asp:Label runat="server" ID="lblFName" Text='<%# Bind("FirstName") %>'>
</asp:Label></ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="LastName">
<ItemTemplate>
<asp:Label runat="server" ID="lblLName" Text='<%# Bind("LastName") %>'>
</asp:Label></ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</td>
</tr>
</table>
</div>
</center>
</form>
</body>
</html>
Step 3: The complete code of WebForm1.aspx.cs looks as below
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
namespace BindGridViewswithSessionApp
{
public partial class WebForm1 : System.Web.UI.Page
{
SqlConnection con = new SqlConnection("Data Source=WIN-KV3BO1RQQF7;Initial Catalog=Company;Integrated Security=True");
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Bind();
}
}
protected void Button1_Click(object sender, EventArgs e)
{
SendGridInfo();
}
private void Bind()
{
con.Open(); SqlCommand cmd = new SqlCommand("select FirstName, LastName from Employee", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet(); da.Fill(ds); con.Close();
GridView1.DataSource = ds; GridView1.DataBind();
}
private void SendGridInfo()
{
DataTable dt = new DataTable();
DataRow dr; dt.Columns.Add(new System.Data.DataColumn("FirstName", typeof(String)));
dt.Columns.Add(new System.Data.DataColumn("LastName", typeof(String)));
foreach (GridViewRow row in GridView1.Rows)
{
Label lblFName = (Label)row.FindControl("lblFName");
Label lblLName = (Label)row.FindControl("lblLName");
dr = dt.NewRow(); dr[0] = lblFName.Text; dr[1] = lblLName.Text; dt.Rows.Add(dr);
}
Session["GridData"] = dt; Response.Redirect("WebForm2.aspx");
}
}
}
Step 4: The complete code of WebForm2.aspx looks as below
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm2.aspx.cs" Inherits="BindGridViewswithSessionApp.WebForm2" %>
<!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 id="Head1" runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<center>
<div>
<table>
<tr>
<td colspan="2" align="center">
<asp:GridView ID="GridView1" runat="server" CssClass="grid" BackColor="LightGoldenrodYellow"
BorderColor="Tan" BorderWidth="1px" CellPadding="2" ForeColor="Black" GridLines="None">
<AlternatingRowStyle BackColor="PaleGoldenrod" />
<FooterStyle BackColor="Tan" />
<HeaderStyle BackColor="Tan" Font-Bold="True" />
<PagerStyle BackColor="PaleGoldenrod" ForeColor="DarkSlateBlue" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="DarkSlateBlue" ForeColor="GhostWhite" />
<SortedAscendingCellStyle BackColor="#FAFAE7" />
<SortedAscendingHeaderStyle BackColor="#DAC09E" />
<SortedDescendingCellStyle BackColor="#E1DB9C" />
<SortedDescendingHeaderStyle BackColor="#C2A47B" />
</asp:GridView>
</td>
</tr>
</table>
</div>
</center>
</form>
</body>
</html>
Step 5: The complete code of WebForm2.aspx.cs looks as below
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace BindGridViewswithSessionApp
{
public partial class WebForm2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
if (Session["GridData"] != null)
{
GridView1.DataSource = Session["GridData"]; GridView1.DataBind();
}
}
}
}
}
Step 6: The output for the application are shown below:

Step 7: Binding gridview of another page output for the application to are
shown below:


Join the conversation! Your thoughts help the community grow.