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">
<Columns>
<asp:BoundField DataField="pid" HeaderText="product id." />
<asp:BoundField DataField="pname" HeaderText="product Name" />
<asp:BoundField DataField="price" HeaderText="price" />
<asp:TemplateField HeaderText="Details">
<ItemTemplate>
<asp:LinkButton ID="lnkDet" CommandName="cmdBind" CommandArgument="<%# ((GridViewRow) Container).RowIndex %>"
runat="server" CausesValidation="false">select product</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:GridView ID="GridView2" runat="server" DataKeyNames="pid" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="pid" HeaderText="product id." />
<asp:BoundField DataField="pname" HeaderText="product Name" />
<asp:BoundField DataField="price" HeaderText="price" />
</Columns>
</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)
{
GridData();
dt1.Columns.Add("pid");
dt1.Columns.Add("pname");
dt1.Columns.Add("price");
Session["dt"] = dt1;
}
}
//Refresh gridview using below method
void GridData()
{
sqlcmd = new SqlCommand("select * from product", sqlcon);
sqlcon.Open();
da = new SqlDataAdapter(sqlcmd);
dt.Clear();
da.Fill(dt);
if (dt.Rows.Count > 0)
{
GridView1.DataSource = dt;
GridView1.DataBind();
}
}
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
GridData();
}
//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;
dt1.Rows.Add(dr);
Session["dt"] = dt1;
GridView2.DataSource = dt1;
GridView2.DataBind();
}
}
}
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.
Very Very UseFul Demonstration ...its very useful for me..thanks a lot Mr. Ravindran