This article we explain how we can pass the selected rows of the grid view to next page and bind it to the grid view on Second Page
Introduction
In the previous article of
Pass Selected Rows of Grid View to another Grid View Dynamically we saw that we can get selected records of grid view and bind it to another grid view. But there can be a requirement where we need to bind selected records of The Grid View of Page 1 to the Grid View of Page 2.
Objective
- Passing the Selected Records of the Grid View to another Page and Bind it to the Grid View of another page.
Using the code
- We have already created a Website in the previous article.So Over here we will add a New Web form and drag and drop a Grid View and Add a data Column Named Country.
- Now we will go to the Previous Page i.e. Page 1 and make changes to the code on the button click event of Pass Selected Records Button.
Lets Start Implementation
// Tables and Stored Procedure
USE [DNF]
GO
/****** Object: Table [dbo].[Countries] Script Date: 07/22/2013 22:41:53 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[Countries](
[CountryID] [int] IDENTITY(1,1) NOT NULL,
[Countries] [varchar](25) NULL
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO -- Proc for getting Countries
create proc [dbo].[GetCountriesList]
as
begin
select * from Countries
end
GO
HTML Mark Up for Page 1
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="PassingGridViewSample.aspx.cs"
Inherits="PassingGridViewSample" %>
<!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>
<style type="text/css">
.style1
{
font-family: "Trebuchet MS";
font-size: x-large;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div class="style1" style="text-align: center">
Selecting Grid View Rows and Passing it to another Grid View
Page - Part 2</div>
<br />
<br />
<div align="center">
<asp:GridView ID="xdata" runat="server" HeaderStyle-BackColor="Bisque" AutoGenerateColumns="False">
<Columns>
<asp:TemplateField HeaderText="Select">
<ItemTemplate>
<asp:CheckBox ID="chkSelect" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="Countries" HeaderText="Countries" />
</Columns>
</asp:GridView>
</div>
<br />
<br />
<div align="center">
<asp:Button ID="btnGetSelectedRecords" runat="server"
Text="Pass Selected Records to Grid View"
onclick="btnGetSelectedRecords_Click" />
</div>
<br />
<br />
</div>
</form>
</body>
</html>
// Code Behind
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.Data.SqlClient;
public partial class PassingGridViewSample : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindGrid();
}
}
private void BindGrid()
{
try
{
// Function to bind the data to Grid.
SqlConnection con = new SqlConnection("Data Source=AG-KKC;Initial Catalog=DNF;Persist Security Info=True;User ID=sa;Password=sqluser");
con.Open();
SqlCommand getcontent = new SqlCommand("GetCountriesList", con);
getcontent.CommandType = CommandType.StoredProcedure;
SqlDataAdapter da = new SqlDataAdapter(getcontent);
DataSet ds = new DataSet();
da.Fill(ds);
xdata.DataSource = ds;
xdata.DataBind();
}
catch (Exception ex)
{
}
}
protected void btnGetSelectedRecords_Click(object sender, EventArgs e)
{
// creation of data table
DataTable dtgetselectedRecords = new DataTable();
// Adding Dynamic Column to the datatable using the add range method
dtgetselectedRecords.Columns.AddRange(new DataColumn[1] {new DataColumn("Countries")});
// Running a For Loop accross the First Grid View
foreach (GridViewRow gvrow in xdata.Rows)
{
if (gvrow.RowType == DataControlRowType.DataRow)
{
//Finding the Checkbox in the Gridview
CheckBox chkSelect = (gvrow.Cells[0].FindControl("chkSelect") as CheckBox);
// Checking which checkbox are selected
if (chkSelect.Checked)
{
// assigning the records to a string to the cells
string Countries = gvrow.Cells[1].Text;
// Adding the Rows to the datatable
dtgetselectedRecords.Rows.Add(Countries);
}
}
}
// Adding the Data table to Session for passing it to another page.
Session.Add("dtgetselectedRecords", dtgetselectedRecords);
Server.Transfer("~/Page2.aspx");
}
}
// Page 2 HTML Mark Up
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Page2.aspx.cs" Inherits="Page2" %>
<!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>
<style type="text/css">
.style1
{
font-family: "Trebuchet MS";
font-size: x-large;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div class="style1" align="center">
Getting Selected Rows from Previous Page to this page
using Session Object
</div>
<br />
<br />
<div align="center">
<asp:GridView ID="xSelected" runat="server" HeaderStyle-BackColor="Chartreuse" AutoGenerateColumns="False">
<Columns>
<asp:BoundField DataField="Countries" HeaderText="Countries" />
</Columns>
</asp:GridView>
</div>
</form>
</body>
</html>
// Code Behind of Page 2
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
public partial class Page2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{ // Over here we fetch the datatable from session i.e. stored in previous page and //passed here
DataTable dtselectedRows = Session["dtgetselectedRecords"] as DataTable;
xSelected.DataSource = dtselectedRows;
xSelected.DataBind();
}
}
}
Explanation of the Pass Selected Records
- First we will create a data table
- Then we add data columns to the data table using add column range method and specify the name of the column
- Now we will run a For Loop through all the rows in the First Grid and find the check box control in the first grid using findcontrol method.
- Once we find the check box we will check the which check box are selected and add the cells to a string variable
- Finally we add the row to the datatable using add row method.
- Then we use Session.Add Property to add the data table and redirect the page to page2
- Now on Page2 on the Page Load event we fetch the data table from Session and assign to the Data grid as its Source and Bind it.
Output 1 : Selecting the Records
Output 2 : Passing the Selected Record to Page 2
Conclusion
I owe thanks to the Moderator of Articles Section for giving an Idea to extend my article to Second Part.