Select Record from one Gridview and show in other Gridview
.aspx code:
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<style type="text/css">
.FooterStyle
{
background-color: #a33;
color: White;
text-align: right;
}
</style>
<script type = "text/javascript">
<!--
function Check_Click(objRef) {
//Get the Row based on checkbox
var row = objRef.parentNode.parentNode;
//Get the reference of GridView
var GridView = row.parentNode;
//Get all input elements in Gridview
var inputList = GridView.getElementsByTagName("input");
for (var i = 0; i < inputList.length; i++) {
//The First element is the Header Checkbox
var headerCheckBox = inputList[0];
//Based on all or none checkboxes
//are checked check/uncheck Header Checkbox
var checked = true;
if (inputList[i].type == "checkbox" && inputList[i] != headerCheckBox) {
if (!inputList[i].checked) {
checked = false;
break;
}
}
}
headerCheckBox.checked = checked;
}
function checkAll(objRef) {
var GridView = objRef.parentNode.parentNode.parentNode;
var inputList = GridView.getElementsByTagName("input");
for (var i = 0; i < inputList.length; i++) {
var row = inputList[i].parentNode.parentNode;
if (inputList[i].type == "checkbox" && objRef != inputList[i]) {
if (objRef.checked) {
inputList[i].checked = true;
}
else {
if (row.rowIndex % 2 == 0) {
row.style.backgroundColor = "#C2D69B";
}
else {
row.style.backgroundColor = "white";
}
inputList[i].checked = false;
}
}
}
}
//-->
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<div>
<asp:GridView ID="GV1" runat="server"
AutoGenerateColumns = "false" Font-Names = "Arial"
Font-Size = "11pt" AlternatingRowStyle-BackColor = "#C2D69B"
HeaderStyle-BackColor = "green" AllowPaging ="true"
OnPageIndexChanging = "OnPaging" PageSize = "10" Width="345px"
ShowFooter="true" FooterStyle-CssClass="FooterStyle"
onrowdatabound="GV1_RowDataBound" >
<Columns>
<asp:TemplateField>
<HeaderTemplate>
<asp:CheckBox ID="chkAll" runat="server" onclick = "checkAll(this);"
AutoPostBack = "true" OnCheckedChanged = "CheckBox_CheckChanged" />
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox ID="chk" runat="server" onclick = "Check_Click(this)"
AutoPostBack = "true" OnCheckedChanged = "CheckBox_CheckChanged" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField = "emp_id" HeaderText = "Emp Id" HtmlEncode = "false" />
<asp:BoundField DataField = "emp_name" HeaderText = "Emp Name" HtmlEncode = "false" />
<asp:BoundField DataField = "phone" HeaderText = "Phone" HtmlEncode = "false" />
<asp:BoundField HeaderText="Salary" DataField="Salary" />
</Columns>
<AlternatingRowStyle BackColor="#C2D69B" />
<HeaderStyle BackColor="Green"></HeaderStyle>
</asp:GridView>
<hr />
<div>
<asp:GridView ID="GVSelected" runat="server"
AutoGenerateColumns = "false" Font-Names = "Arial"
Font-Size = "11pt" AlternatingRowStyle-BackColor = "#C2D69B"
HeaderStyle-BackColor = "green" EmptyDataText = "No Records Selected"
Width="346px" >
<Columns>
<asp:BoundField DataField = "emp_id" HeaderText = "Emp Id" HtmlEncode = "false" />
<asp:BoundField DataField = "emp_name" HeaderText = "Emp Name" HtmlEncode = "false" />
<asp:BoundField DataField = "phone" HeaderText = "Phone" HtmlEncode = "false" />
<asp:BoundField HeaderText="Salary" DataField="Salary" />
</Columns>
</asp:GridView>
</div>
</div>
</div>
</form>
</body>
</html>
aspx.cs code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
public partial class Gridview2 : System.Web.UI.Page
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["conn"].ToString());
SqlDataAdapter da;
DataSet ds = new DataSet();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
grid_bind();
}
}
public void grid_bind()
{
try
{
con.Open();
da = new SqlDataAdapter("select *from skgridview1", con);
da.Fill(ds);
GV1.DataSource = ds;
GV1.DataBind();
con.Close();
}
catch (Exception ex)
{
throw ex;
}
}
private void BindSeconGrid()
{
DataTable dt = (DataTable)ViewState["SelectedRecords"];
GVSelected.DataSource = dt;
GVSelected.DataBind();
}
private void GetData()
{
DataTable dt;
if (ViewState["SelectedRecords"] != null)
dt = (DataTable)ViewState["SelectedRecords"];
else
dt = CreateDataTable();
CheckBox chkAll = (CheckBox)GV1.HeaderRow.Cells[0].FindControl("chkAll");
for (int i = 0; i < GV1.Rows.Count; i++)
{
if (chkAll.Checked)
{
dt = AddRow(GV1.Rows[i], dt);
}
else
{
CheckBox chk = (CheckBox)GV1.Rows[i].Cells[0].FindControl("chk");
if (chk.Checked)
{
dt = AddRow(GV1.Rows[i], dt);
}
else
{
dt = RemoveRow(GV1.Rows[i], dt);
}
}
}
ViewState["SelectedRecords"] = dt;
}
private void SetData()
{
CheckBox chkAll = (CheckBox)GV1.HeaderRow.Cells[0].FindControl("chkAll");
chkAll.Checked = true;
if (ViewState["SelectedRecords"] != null)
{
DataTable dt = (DataTable)ViewState["SelectedRecords"];
for (int i = 0; i <= GV1.Rows.Count - 1; i++)
{
CheckBox chk = (CheckBox)GV1.Rows[i].Cells[0].FindControl("chk");
if (chk != null)
{
DataRow[] dr = dt.Select("emp_id = '" + GV1.Rows[i].Cells[1].Text + "'");
chk.Checked = dr.Length > 0;
if (!chk.Checked)
{
chkAll.Checked = false;
}
}
}
}
}
private DataTable CreateDataTable()
{
DataTable dt = new DataTable();
dt.Columns.Add("emp_id");
dt.Columns.Add("emp_name");
dt.Columns.Add("phone");
dt.Columns.Add("Salary");
dt.AcceptChanges();
return dt;
}
private DataTable AddRow(GridViewRow gvRow, DataTable dt)
{
DataRow[] dr = dt.Select("emp_id = '" + gvRow.Cells[1].Text + "'");
if (dr.Length <= 0)
{
dt.Rows.Add();
dt.Rows[dt.Rows.Count - 1]["emp_id"] = gvRow.Cells[1].Text;
dt.Rows[dt.Rows.Count - 1]["emp_name"] = gvRow.Cells[2].Text;
dt.Rows[dt.Rows.Count - 1]["phone"] = gvRow.Cells[3].Text;
dt.Rows[dt.Rows.Count - 1]["Salary"] = gvRow.Cells[4].Text;
dt.AcceptChanges();
}
return dt;
}
private DataTable RemoveRow(GridViewRow gvRow, DataTable dt)
{
DataRow[] dr = dt.Select("emp_id = '" + gvRow.Cells[1].Text + "'");
if (dr.Length > 0)
{
dt.Rows.Remove(dr[0]);
dt.AcceptChanges();
}
return dt;
}
protected void GV1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GetData();
GV1.PageIndex = e.NewPageIndex;
grid_bind();
SetData();
}
protected void chk_CheckedChanged(object sender, EventArgs e)
{
GetData();
SetData();
BindSeconGrid();
}
protected void OnPaging(object sender, GridViewPageEventArgs e)
{
GetData();
GV1.PageIndex = e.NewPageIndex;
grid_bind();
SetData();
}
protected void CheckBox_CheckChanged(object sender, EventArgs e)
{
GetData();
SetData();
BindSeconGrid();
}
}
Demo
.aspx code:
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<style type="text/css">
.FooterStyle
{
background-color: #a33;
color: White;
text-align: right;
}
</style>
<script type = "text/javascript">
<!--
function Check_Click(objRef) {
//Get the Row based on checkbox
var row = objRef.parentNode.parentNode;
//Get the reference of GridView
var GridView = row.parentNode;
//Get all input elements in Gridview
var inputList = GridView.getElementsByTagName("input");
for (var i = 0; i < inputList.length; i++) {
//The First element is the Header Checkbox
var headerCheckBox = inputList[0];
//Based on all or none checkboxes
//are checked check/uncheck Header Checkbox
var checked = true;
if (inputList[i].type == "checkbox" && inputList[i] != headerCheckBox) {
if (!inputList[i].checked) {
checked = false;
break;
}
}
}
headerCheckBox.checked = checked;
}
function checkAll(objRef) {
var GridView = objRef.parentNode.parentNode.parentNode;
var inputList = GridView.getElementsByTagName("input");
for (var i = 0; i < inputList.length; i++) {
var row = inputList[i].parentNode.parentNode;
if (inputList[i].type == "checkbox" && objRef != inputList[i]) {
if (objRef.checked) {
inputList[i].checked = true;
}
else {
if (row.rowIndex % 2 == 0) {
row.style.backgroundColor = "#C2D69B";
}
else {
row.style.backgroundColor = "white";
}
inputList[i].checked = false;
}
}
}
}
//-->
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<div>
<asp:GridView ID="GV1" runat="server"
AutoGenerateColumns = "false" Font-Names = "Arial"
Font-Size = "11pt" AlternatingRowStyle-BackColor = "#C2D69B"
HeaderStyle-BackColor = "green" AllowPaging ="true"
OnPageIndexChanging = "OnPaging" PageSize = "10" Width="345px"
ShowFooter="true" FooterStyle-CssClass="FooterStyle"
onrowdatabound="GV1_RowDataBound" >
<Columns>
<asp:TemplateField>
<HeaderTemplate>
<asp:CheckBox ID="chkAll" runat="server" onclick = "checkAll(this);"
AutoPostBack = "true" OnCheckedChanged = "CheckBox_CheckChanged" />
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox ID="chk" runat="server" onclick = "Check_Click(this)"
AutoPostBack = "true" OnCheckedChanged = "CheckBox_CheckChanged" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField = "emp_id" HeaderText = "Emp Id" HtmlEncode = "false" />
<asp:BoundField DataField = "emp_name" HeaderText = "Emp Name" HtmlEncode = "false" />
<asp:BoundField DataField = "phone" HeaderText = "Phone" HtmlEncode = "false" />
<asp:BoundField HeaderText="Salary" DataField="Salary" />
</Columns>
<AlternatingRowStyle BackColor="#C2D69B" />
<HeaderStyle BackColor="Green"></HeaderStyle>
</asp:GridView>
<hr />
<div>
<asp:GridView ID="GVSelected" runat="server"
AutoGenerateColumns = "false" Font-Names = "Arial"
Font-Size = "11pt" AlternatingRowStyle-BackColor = "#C2D69B"
HeaderStyle-BackColor = "green" EmptyDataText = "No Records Selected"
Width="346px" >
<Columns>
<asp:BoundField DataField = "emp_id" HeaderText = "Emp Id" HtmlEncode = "false" />
<asp:BoundField DataField = "emp_name" HeaderText = "Emp Name" HtmlEncode = "false" />
<asp:BoundField DataField = "phone" HeaderText = "Phone" HtmlEncode = "false" />
<asp:BoundField HeaderText="Salary" DataField="Salary" />
</Columns>
</asp:GridView>
</div>
</div>
</div>
</form>
</body>
</html>
aspx.cs code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
public partial class Gridview2 : System.Web.UI.Page
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["conn"].ToString());
SqlDataAdapter da;
DataSet ds = new DataSet();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
grid_bind();
}
}
public void grid_bind()
{
try
{
con.Open();
da = new SqlDataAdapter("select *from skgridview1", con);
da.Fill(ds);
GV1.DataSource = ds;
GV1.DataBind();
con.Close();
}
catch (Exception ex)
{
throw ex;
}
}
private void BindSeconGrid()
{
DataTable dt = (DataTable)ViewState["SelectedRecords"];
GVSelected.DataSource = dt;
GVSelected.DataBind();
}
private void GetData()
{
DataTable dt;
if (ViewState["SelectedRecords"] != null)
dt = (DataTable)ViewState["SelectedRecords"];
else
dt = CreateDataTable();
CheckBox chkAll = (CheckBox)GV1.HeaderRow.Cells[0].FindControl("chkAll");
for (int i = 0; i < GV1.Rows.Count; i++)
{
if (chkAll.Checked)
{
dt = AddRow(GV1.Rows[i], dt);
}
else
{
CheckBox chk = (CheckBox)GV1.Rows[i].Cells[0].FindControl("chk");
if (chk.Checked)
{
dt = AddRow(GV1.Rows[i], dt);
}
else
{
dt = RemoveRow(GV1.Rows[i], dt);
}
}
}
ViewState["SelectedRecords"] = dt;
}
private void SetData()
{
CheckBox chkAll = (CheckBox)GV1.HeaderRow.Cells[0].FindControl("chkAll");
chkAll.Checked = true;
if (ViewState["SelectedRecords"] != null)
{
DataTable dt = (DataTable)ViewState["SelectedRecords"];
for (int i = 0; i <= GV1.Rows.Count - 1; i++)
{
CheckBox chk = (CheckBox)GV1.Rows[i].Cells[0].FindControl("chk");
if (chk != null)
{
DataRow[] dr = dt.Select("emp_id = '" + GV1.Rows[i].Cells[1].Text + "'");
chk.Checked = dr.Length > 0;
if (!chk.Checked)
{
chkAll.Checked = false;
}
}
}
}
}
private DataTable CreateDataTable()
{
DataTable dt = new DataTable();
dt.Columns.Add("emp_id");
dt.Columns.Add("emp_name");
dt.Columns.Add("phone");
dt.Columns.Add("Salary");
dt.AcceptChanges();
return dt;
}
private DataTable AddRow(GridViewRow gvRow, DataTable dt)
{
DataRow[] dr = dt.Select("emp_id = '" + gvRow.Cells[1].Text + "'");
if (dr.Length <= 0)
{
dt.Rows.Add();
dt.Rows[dt.Rows.Count - 1]["emp_id"] = gvRow.Cells[1].Text;
dt.Rows[dt.Rows.Count - 1]["emp_name"] = gvRow.Cells[2].Text;
dt.Rows[dt.Rows.Count - 1]["phone"] = gvRow.Cells[3].Text;
dt.Rows[dt.Rows.Count - 1]["Salary"] = gvRow.Cells[4].Text;
dt.AcceptChanges();
}
return dt;
}
private DataTable RemoveRow(GridViewRow gvRow, DataTable dt)
{
DataRow[] dr = dt.Select("emp_id = '" + gvRow.Cells[1].Text + "'");
if (dr.Length > 0)
{
dt.Rows.Remove(dr[0]);
dt.AcceptChanges();
}
return dt;
}
protected void GV1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GetData();
GV1.PageIndex = e.NewPageIndex;
grid_bind();
SetData();
}
protected void chk_CheckedChanged(object sender, EventArgs e)
{
GetData();
SetData();
BindSeconGrid();
}
protected void OnPaging(object sender, GridViewPageEventArgs e)
{
GetData();
GV1.PageIndex = e.NewPageIndex;
grid_bind();
SetData();
}
protected void CheckBox_CheckChanged(object sender, EventArgs e)
{
GetData();
SetData();
BindSeconGrid();
}
}
Demo
Emp Id | Emp Name | Phone | Salary | |||
---|---|---|---|---|---|---|
57 | reeta | 8965278415 | 25500.0000 | |||
58 | reeta | 8965278415 | 25500.0000 | |||
73 | ram | 4598562105 | 25000.0000 | |||
74 | ram | 4598562105 | 25000.0000 | |||
75 | ram | 4598562105 | 25000.0000 | |||
76 | ram | 4598562105 | 25000.0000 | |||
77 | ram | 4598562105 | 25000.0000 | |||
78 | ram | 4598562105 | 25000.0000 | |||
79 | ram | 4598562105 | 25000.0000 | |||
80 | ram | 4598562105 | 25000.0000 | |||
|
Emp Id | Emp Name | Phone | Salary |
---|---|---|---|
73 | ram | 4598562105 | 25000.0000 |
74 | ram | 4598562105 | 25000.0000 |
76 | ram | 4598562105 | 25000.0000 |
No comments:
Post a Comment