How to send one gridview data to another gridview on the same page


In this article we will discuss how we can send one gridview data to another on the same page and on another page also. I have two pages one is Products and another is ViewCart. Products page shows me how many products are there and which i want to buy and ViewCart page shows me which products i purchased from products page.

Step 1: Create a gridview like this

Products cart view

It will look like :

GridView1 on Products Page

After binding this gridview. add one more gridview and design like this on the same page :

Selected products cart view

It will look like after selecting products :

GridView2 on Products Page


Step 2: Write this code on code behind file


using System;
using System.Data;
using System.Data.SqlClient;

// Create a sql connection
SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\v11.0;AttachDbFilename=D:\Online Shopping\App_Data\Shopping.mdf;Integrated Security=True;Pooling=True");
// Modify your connecting string as per your system
DataTable dt = new DataTable();
DataTable dt1 = new DataTable();
DataRow dr;


Write this code on Gridview Row Command event :

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
// Command for binding data to gridview
if (e.CommandName == "cmdBind")
{
LinkButton lb = (LinkButton)e.CommandSource;
int index = Convert.ToInt32(lb.CommandArgument);
dt1 = (DataTable)Session["dt"];
dr = dt1.NewRow();
dr["ProductID"] = GridView1.Rows[index].Cells[1].Text;
dr["Category"] = GridView1.Rows[index].Cells[2].Text;
dr["BrandName"] = GridView1.Rows[index].Cells[3].Text;
dr["Description"] = GridView1.Rows[index].Cells[4].Text;
dr["UnitPrice"] = GridView1.Rows[index].Cells[5].Text;
dt1.Rows.Add(dr);
Session["dt"] = dt1;
GridView2.DataSource = dt1;
GridView2.DataBind();
addall.Visible = true;
}
}


Write this code on button click event of add selected products to cart to transfer all selected products to the View Cart page :

protected void addall_Click(object sender, EventArgs e)
{
// Server.Transfer method use because it helps to transfer objects to another page
Server.Transfer("ViewCart.aspx",true);
}


Step 3: Create a Gridview on ViewCart.aspx page like this

Final cart view


Step 4: Write this code in ViewCart.aspx.cs page

protected void Page_Load(object sender, EventArgs e)
{
// Binding selected products to the cart
GridView1.DataSource = Session["dt"];
GridView1.DataBind();
}


Now you got your selected products on the another page.

It will look like :

GridView1 on ViewCart Page


Article by Nirav Lalan
Regards, Nirav Lalan DNS Gold Member "If you can dream it, you can do it."

Follow Nirav Lalan or read 17 articles authored by Nirav Lalan

Comments

No responses found. Be the first to comment...


  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: