Here is an example to access
GridView
from previous page. In first page set
DataSource
of the
GridView
to a
DataTable
using
ds.Tables[0]
. Then in
Button
click event add this
DataTable
in
Session
. In second page, get this
DataTable
from the session and bind it to
GridView
in second page:
In first page add
GridView
data source in session:
protected void Page_Load(object sender, EventArgs e)
{
string ConString = ConfigurationManager.ConnectionStrings["ConString2"].ConnectionString;
SqlConnection con = new SqlConnection(ConString);
string CmdString = "SELECT * FROM Departments";
SqlCommand cmd = new SqlCommand(CmdString, con);
SqlDataAdapter sda = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
sda.Fill(ds);
GridView2.DataSource = ds.Tables[0];
GridView2.DataBind();
}
protected void Button1_Click(object sender, EventArgs e)
{
DataTable dt = new DataTable();
dt = (DataTable)GridView2.DataSource;
Session.Add("MyDataTable", dt);
Response.Redirect("WebForm2.aspx");
}
In second page get Data Source from the session:
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
DataTable dt = new DataTable();
dt = (DataTable)Session["MyDataTable"];
GridView1.DataSource = dt;
GridView1.DataBind();
}
}
Another solution is here:
http://www.aspsnippets.com/Articles/Pass-ASP.Net-GridView-from-one-page-to-another-page.aspx[
^]