<%@ Page Language="C#" AutoEventWireup="true" CodeFile="GridForm.aspx.cs" Inherits="GridForm" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="gvEmpInfo" AutoGenerateColumns="False" CellPadding="4" runat="server"
BackColor="White" BorderColor="#3366CC" BorderStyle="None" BorderWidth="1px">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="chkbox" runat="server" AutoPostBack="true" OnCheckedChanged="ChckedChanged" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField HeaderText="EmpId" DataField="EmpId" />
<asp:BoundField HeaderText="EmpName" DataField="Emp_Name" />
<asp:BoundField HeaderText="EmpAddress" DataField="Emp_Address" />
<asp:BoundField HeaderText="EmpDept" DataField="Emp_Department" />
</Columns>
<FooterStyle BackColor="#99CCCC" ForeColor="#003399" />
<HeaderStyle BackColor="#003399" Font-Bold="true" ForeColor="#CCCCFF" />
<PagerStyle BackColor="#99CCCC" ForeColor="#003399" HorizontalAlign="Left" />
<RowStyle BackColor="White" ForeColor="#003399" />
<SelectedRowStyle BackColor="#009999" Font-Bold="True" ForeColor="#CCFF99" />
<SortedAscendingCellStyle BackColor="#EDF6F6" />
<SortedAscendingHeaderStyle BackColor="#0D4AC4" />
<SortedDescendingCellStyle BackColor="#D6DFDF" />
<SortedDescendingHeaderStyle BackColor="#002876" />
</asp:GridView>
<br />
<b>OnCheckedChanged Event Fired on Gridview</b>
<asp:GridView ID="gvMovedRows" AutoGenerateColumns="False" CellPadding="4"
runat="server" ForeColor="#333333" GridLines="None">
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
<Columns>
<asp:BoundField HeaderText="EmpId" DataField="EmpId" />
<asp:BoundField HeaderText="EmpName" DataField="Emp_Name" />
<asp:BoundField HeaderText="EmpAddress" DataField="Emp_Address" />
<asp:BoundField HeaderText="EmpDept" DataField="Emp_Department" />
</Columns>
<EditRowStyle BackColor="#999999" />
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="true" ForeColor="White" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#E9E7E2" />
<SortedAscendingHeaderStyle BackColor="#506C8C" />
<SortedDescendingCellStyle BackColor="#FFFDF8" />
<SortedDescendingHeaderStyle BackColor="#6F8DAE" />
</asp:GridView>
</div>
</form>
</body>
</html>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class GridForm : System.Web.UI.Page
{
SqlConnection con;
SqlDataAdapter adap;
DataTable dt;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindGridview();
}
}
public GridForm()
{
con = new SqlConnection(@"Data Source=.;Initial Catalog=Employee;User ID=;Password=");
}
protected void BindGridview()
{
adap = new SqlDataAdapter("select * from EmployeeInformation", con);
dt = new DataTable();
adap.Fill(dt);
gvEmpInfo.DataSource = dt;
gvEmpInfo.DataBind();
}
protected void ChckedChanged(object sender, EventArgs e)
{
GetSelectedRows();
BindSecondGrid();
}
protected void BindSecondGrid()
{
DataTable dt = (DataTable)ViewState["EmpDetails"];
gvMovedRows.DataSource = dt;
gvMovedRows.DataBind();
}
private void GetSelectedRows()
{
DataTable dt;
if (ViewState["EmpDetails"] != null)
dt = (DataTable)ViewState["EmpDetails"];
else
dt = CreateTable();
for (int i = 0; i < gvEmpInfo.Rows.Count; i++)
{
CheckBox chk = (CheckBox)gvEmpInfo.Rows[i].Cells[0].FindControl("chkbox");
if (chk.Checked)
{
dt = MoveRows(gvEmpInfo.Rows[i], dt);
}
else
{
dt = RemoveRow(gvEmpInfo.Rows[i], dt);
}
}
ViewState["EmpDetails"] = dt;
}
private DataTable CreateTable()
{
DataTable dt = new DataTable();
dt.Columns.Add("EmpId");
dt.Columns.Add("Emp_Name");
dt.Columns.Add("Emp_Address");
dt.Columns.Add("Emp_Department");
dt.AcceptChanges();
return dt;
}
private DataTable MoveRows(GridViewRow gvRow, DataTable dt)
{
DataRow[] dr = dt.Select("EmpId = '" + gvRow.Cells[1].Text + "'");
if (dr.Length <= 0)
{
dt.Rows.Add();
int rowscount = dt.Rows.Count - 1;
dt.Rows[rowscount]["EmpId"] = gvRow.Cells[1].Text;
dt.Rows[rowscount]["Emp_Name"] = gvRow.Cells[2].Text;
dt.Rows[rowscount]["Emp_Address"] = gvRow.Cells[3].Text;
dt.Rows[rowscount]["Emp_Department"] = gvRow.Cells[4].Text;
dt.AcceptChanges();
}
return dt;
}
private DataTable RemoveRow(GridViewRow gvRow, DataTable dt)
{
DataRow[] dr = dt.Select("EmpId = '" + gvRow.Cells[1].Text + "'");
if (dr.Length > 0)
{
dt.Rows.Remove(dr[0]);
dt.AcceptChanges();
}
return dt;
}
}