How to add selected row from one gridview to another gridview?

In this article, I am going to explain about how to add selected product row from one Gridview to another. This concept is use many times in shopping cart projects. If user is added product in his/her shopping cart then finally need to show selected product in separate grid.

Description :

In many f shopping cart website we are select product in the grid view we need to keep that values. Using below concept to add that product in separate gridview.

Design side

I have placed two gridview controls one for show all product details and other one is used to show selected product details.

<asp:GridView ID="GridView1" runat="server" DataKeyNames="pid" AutoGenerateColumns="false"
OnPageIndexChanging="GridView1_PageIndexChanging" OnRowCommand="GridView1_RowCommand">
<asp:BoundField DataField="pid" HeaderText="product id." />
<asp:BoundField DataField="pname" HeaderText="product Name" />
<asp:BoundField DataField="price" HeaderText="price" />
<asp:TemplateField HeaderText="Details">
<asp:LinkButton ID="lnkDet" CommandName="cmdBind" CommandArgument="<%# ((GridViewRow) Container).RowIndex %>"
runat="server" CausesValidation="false">select product</asp:LinkButton>
<asp:GridView ID="GridView2" runat="server" DataKeyNames="pid" AutoGenerateColumns="false">
<asp:BoundField DataField="pid" HeaderText="product id." />
<asp:BoundField DataField="pname" HeaderText="product Name" />
<asp:BoundField DataField="price" HeaderText="price" />
</asp:GridView><br />

Code behind

I have added each selected row in second gridview as using below code

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

public partial class _Default : System.Web.UI.Page
SqlConnection sqlcon = new SqlConnection(ConfigurationManager.ConnectionStrings["Con"].ConnectionString);
SqlCommand sqlcmd;
SqlDataAdapter da;
DataTable dt = new DataTable();
DataTable dt1 = new DataTable();
DataRow dr;

protected void Page_Load(object sender, EventArgs e)
if (!Page.IsPostBack)
Session["dt"] = dt1;
//Refresh gridview using below method
void GridData()
sqlcmd = new SqlCommand("select * from product", sqlcon);
da = new SqlDataAdapter(sqlcmd);
if (dt.Rows.Count > 0)
GridView1.DataSource = dt;

protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
GridView1.PageIndex = e.NewPageIndex;
//If user click select in gridview then that row valuse insert into the second grid
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
if (e.CommandName == "cmdBind")
LinkButton lb = (LinkButton)e.CommandSource;
int index = Convert.ToInt32(lb.CommandArgument);
dt1 = (DataTable)Session["dt"];
dr = dt1.NewRow();
dr["pid"] = GridView1.Rows[index].Cells[0].Text;
dr["pname"] = GridView1.Rows[index].Cells[1].Text;
dr["price"] = GridView1.Rows[index].Cells[2].Text;
Session["dt"] = dt1;
GridView2.DataSource = dt1;

Here see this image only add selected product into second grid view

Source code:Client Side: ASP.NET

Code Behind: C#

ConclusionI hope that this code snippet helps you to know about copy selected product details from one another gridview.


  • grdDatamove (44355-72956-grdDatamove.rar)
  • Comments

    Guest Author: chaitanya kiran kasa29 Mar 2013

    Very Very UseFul Demonstration ...its very useful for me..thanks a lot Mr. Ravindran

    Guest Author: Santosh02 May 2013

    Can you tell me how the child grid can be displayed as the row of the main grid under the selected row?

    Author: Nirav Lalan23 Jan 2014 Member Level: Gold   Points : 0

    Thank you so much Ravindran sir.

    this post helped me a lot.
    for this thing i was suffering from last month.

    thank you so much again.

  • 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: