In this article, we will learn “how to move Selected Gridview Records to another Gridview”. In some situations, we need to move or transfer records from one gridview to another gridview. We can accomplish this task by following the below method. First of all, design an aspx webpage like below.<%@ Page Language="C#" AutoEventWireup="true" CodeFile="frmGridMoveToGrid.aspx.cs" Inherits="frmGridMoveToGrid" %>
<!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>Move Selected GridView Rows To Another GridView</title>
</head>
<body>
<form id="form1" runat="server">
<div align="center">
<table>
<tr>
<td>
<asp:GridView ID="grdFirst" runat="server" AutoGenerateColumns="False">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="chkSelect" runat="server" OnCheckedChanged="chkSelect_CheckChanged" AutoPostBack="true" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="UserId" HeaderText="UserId" />
<asp:BoundField DataField="UserName" HeaderText="UserName" />
<asp:BoundField DataField="Education" HeaderText="Education" />
<asp:BoundField DataField="Location" HeaderText="Location" />
</Columns>
<RowStyle BackColor="#E0E0E0" />
<HeaderStyle BackColor="#FF8000" />
<AlternatingRowStyle BackColor="#FFC0C0" />
</asp:GridView>
</td>
</tr>
<tr>
<td></td>
</tr>
<tr>
<td>
<asp:GridView ID="grdSecond" runat="server" AutoGenerateColumns="False">
<Columns>
<asp:BoundField DataField="UserId" HeaderText="UserId" />
<asp:BoundField DataField="UserName" HeaderText="UserName" />
<asp:BoundField DataField="Education" HeaderText="Education" />
<asp:BoundField DataField="Location" HeaderText="Location" />
</Columns>
<RowStyle BackColor="#80FFFF" />
<HeaderStyle BackColor="#8080FF" />
<EditRowStyle BackColor="White" />
<AlternatingRowStyle BackColor="#C0C000" />
</asp:GridView>
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
In the above code, we have placed two gridviews, one is for binding the table and the second one is for displaying the records based on selection in first gridview. It is clear that we have taken one Template field in which we need to place CheckBox in order to give provision for selection.
In Code-Behind file, write down the below code.
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
public partial class frmGridMoveToGrid : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindGridFirst();
BindGridSecond();
}
}
private void BindGridFirst()
{
DataTable dt = new DataTable();
dt.Columns.Add("UserId", typeof(Int32));
dt.Columns.Add("UserName", typeof(string));
dt.Columns.Add("Education", typeof(string));
dt.Columns.Add("Location", typeof(string));
DataRow dr = dt.NewRow();
dr["UserId"] = "1";
dr["UserName"] = "Prakash";
dr["Education"] = "MCA";
dr["Location"] = "Khairatabad";
dt.Rows.Add(dr);
dr = dt.NewRow();
dr["UserId"] = "2";
dr["UserName"] = "Manoj";
dr["Education"] = "MCA";
dr["Location"] = "DSNR";
dt.Rows.Add(dr);
dr = dt.NewRow();
dr["UserId"] = "3";
dr["UserName"] = "Krishna";
dr["Education"] = "Tech";
dr["Location"] = "KPHB";
dt.Rows.Add(dr);
grdFirst.DataSource = dt;
grdFirst.DataBind();
}
private void BindGridSecond()
{
DataTable dt = (DataTable)ViewState["GetRecords"];
grdSecond.DataSource = dt;
grdSecond.DataBind();
}
protected void chkSelect_CheckChanged(object sender, EventArgs e)
{
GetSelectedRows();
BindGridSecond();
}
private void GetSelectedRows()
{
DataTable dt;
if (ViewState["GetRecords"] != null)
dt = (DataTable)ViewState["GetRecords"];
else
dt = CreateTable();
for (int i = 0; i < grdFirst.Rows.Count; i++)
{
CheckBox chk = (CheckBox)grdFirst.Rows[i].Cells[0].FindControl("chkSelect");
if (chk.Checked)
{
dt = AddGridRow(grdFirst.Rows[i], dt);
}
else
{
dt = RemoveGridRow(grdFirst.Rows[i], dt);
}
}
ViewState["GetRecords"] = dt;
}
private DataTable CreateTable()
{
DataTable dt = new DataTable();
dt.Columns.Add("UserId");
dt.Columns.Add("UserName");
dt.Columns.Add("Education");
dt.Columns.Add("Location");
dt.AcceptChanges();
return dt;
}
private DataTable AddGridRow(GridViewRow row, DataTable dt)
{
DataRow[] dr = dt.Select("UserId = '" + row.Cells[1].Text + "'");
if (dr.Length <= 0)
{
dt.Rows.Add();
int rowscount = dt.Rows.Count - 1;
dt.Rows[rowscount]["UserId"] = row.Cells[1].Text;
dt.Rows[rowscount]["UserName"] = row.Cells[2].Text;
dt.Rows[rowscount]["Education"] = row.Cells[3].Text;
dt.Rows[rowscount]["Location"] = row.Cells[4].Text;
dt.AcceptChanges();
}
return dt;
}
private DataTable RemoveGridRow(GridViewRow row, DataTable dt)
{
DataRow[] dr = dt.Select("UserId = '" + row.Cells[1].Text + "'");
if (dr.Length > 0)
{
dt.Rows.Remove(dr[0]);
dt.AcceptChanges();
}
return dt;
}
}
Now execute the page and you will observe once we select any checkbox in First Gridview that row was displayed in the second gridview and if we de-select the same row that row was automatically removed from the second gridview. In this way, we can easily move or transfer selected records from one gridview to another gridview.
Happy coding….
How to Create Image Slider in Asp.Net using jQuery
How to Disable Copy/Cut/Paste Options in TextBox
How to Implement Scrollable Grid with Fixed Header in Asp.Net using jQuery ?
How to Set Default Home Page in Internet Explorer or Mozilla Browser
How to Read and Write Connection String in Web.Config
How to Disable Browser Back Button using Javascript in ASP.Net
How to Display Excel data in Gridview using Asp.Net
Dynamically Add Check Boxes in Asp.Net Web Page
<!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>Move Selected GridView Rows To Another GridView</title>
</head>
<body>
<form id="form1" runat="server">
<div align="center">
<table>
<tr>
<td>
<asp:GridView ID="grdFirst" runat="server" AutoGenerateColumns="False">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="chkSelect" runat="server" OnCheckedChanged="chkSelect_CheckChanged" AutoPostBack="true" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="UserId" HeaderText="UserId" />
<asp:BoundField DataField="UserName" HeaderText="UserName" />
<asp:BoundField DataField="Education" HeaderText="Education" />
<asp:BoundField DataField="Location" HeaderText="Location" />
</Columns>
<RowStyle BackColor="#E0E0E0" />
<HeaderStyle BackColor="#FF8000" />
<AlternatingRowStyle BackColor="#FFC0C0" />
</asp:GridView>
</td>
</tr>
<tr>
<td></td>
</tr>
<tr>
<td>
<asp:GridView ID="grdSecond" runat="server" AutoGenerateColumns="False">
<Columns>
<asp:BoundField DataField="UserId" HeaderText="UserId" />
<asp:BoundField DataField="UserName" HeaderText="UserName" />
<asp:BoundField DataField="Education" HeaderText="Education" />
<asp:BoundField DataField="Location" HeaderText="Location" />
</Columns>
<RowStyle BackColor="#80FFFF" />
<HeaderStyle BackColor="#8080FF" />
<EditRowStyle BackColor="White" />
<AlternatingRowStyle BackColor="#C0C000" />
</asp:GridView>
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
In the above code, we have placed two gridviews, one is for binding the table and the second one is for displaying the records based on selection in first gridview. It is clear that we have taken one Template field in which we need to place CheckBox in order to give provision for selection.
In Code-Behind file, write down the below code.
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
public partial class frmGridMoveToGrid : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindGridFirst();
BindGridSecond();
}
}
private void BindGridFirst()
{
DataTable dt = new DataTable();
dt.Columns.Add("UserId", typeof(Int32));
dt.Columns.Add("UserName", typeof(string));
dt.Columns.Add("Education", typeof(string));
dt.Columns.Add("Location", typeof(string));
DataRow dr = dt.NewRow();
dr["UserId"] = "1";
dr["UserName"] = "Prakash";
dr["Education"] = "MCA";
dr["Location"] = "Khairatabad";
dt.Rows.Add(dr);
dr = dt.NewRow();
dr["UserId"] = "2";
dr["UserName"] = "Manoj";
dr["Education"] = "MCA";
dr["Location"] = "DSNR";
dt.Rows.Add(dr);
dr = dt.NewRow();
dr["UserId"] = "3";
dr["UserName"] = "Krishna";
dr["Education"] = "Tech";
dr["Location"] = "KPHB";
dt.Rows.Add(dr);
grdFirst.DataSource = dt;
grdFirst.DataBind();
}
private void BindGridSecond()
{
DataTable dt = (DataTable)ViewState["GetRecords"];
grdSecond.DataSource = dt;
grdSecond.DataBind();
}
protected void chkSelect_CheckChanged(object sender, EventArgs e)
{
GetSelectedRows();
BindGridSecond();
}
private void GetSelectedRows()
{
DataTable dt;
if (ViewState["GetRecords"] != null)
dt = (DataTable)ViewState["GetRecords"];
else
dt = CreateTable();
for (int i = 0; i < grdFirst.Rows.Count; i++)
{
CheckBox chk = (CheckBox)grdFirst.Rows[i].Cells[0].FindControl("chkSelect");
if (chk.Checked)
{
dt = AddGridRow(grdFirst.Rows[i], dt);
}
else
{
dt = RemoveGridRow(grdFirst.Rows[i], dt);
}
}
ViewState["GetRecords"] = dt;
}
private DataTable CreateTable()
{
DataTable dt = new DataTable();
dt.Columns.Add("UserId");
dt.Columns.Add("UserName");
dt.Columns.Add("Education");
dt.Columns.Add("Location");
dt.AcceptChanges();
return dt;
}
private DataTable AddGridRow(GridViewRow row, DataTable dt)
{
DataRow[] dr = dt.Select("UserId = '" + row.Cells[1].Text + "'");
if (dr.Length <= 0)
{
dt.Rows.Add();
int rowscount = dt.Rows.Count - 1;
dt.Rows[rowscount]["UserId"] = row.Cells[1].Text;
dt.Rows[rowscount]["UserName"] = row.Cells[2].Text;
dt.Rows[rowscount]["Education"] = row.Cells[3].Text;
dt.Rows[rowscount]["Location"] = row.Cells[4].Text;
dt.AcceptChanges();
}
return dt;
}
private DataTable RemoveGridRow(GridViewRow row, DataTable dt)
{
DataRow[] dr = dt.Select("UserId = '" + row.Cells[1].Text + "'");
if (dr.Length > 0)
{
dt.Rows.Remove(dr[0]);
dt.AcceptChanges();
}
return dt;
}
}
Now execute the page and you will observe once we select any checkbox in First Gridview that row was displayed in the second gridview and if we de-select the same row that row was automatically removed from the second gridview. In this way, we can easily move or transfer selected records from one gridview to another gridview.
Happy coding….
How to Create Image Slider in Asp.Net using jQuery
How to Disable Copy/Cut/Paste Options in TextBox
How to Implement Scrollable Grid with Fixed Header in Asp.Net using jQuery ?
How to Set Default Home Page in Internet Explorer or Mozilla Browser
How to Read and Write Connection String in Web.Config
How to Disable Browser Back Button using Javascript in ASP.Net
How to Display Excel data in Gridview using Asp.Net
Dynamically Add Check Boxes in Asp.Net Web Page
Related Articles
This post first appeared on Dot Net Programming (C#, Asp.Net, ADO.Net, WCF, WPF, Ajax, LINQ), please read the originial post: here