How to Copy Selected Row One GridView to another GridView
Source Code for Default.aspx………………………………………..
<%@ Page Language="C#"
AutoEventWireup="true"
CodeFile="Default.aspx.cs"
Inherits="_Default"
%>
<!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></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<table width="50%">
<tr><td colspan="2">
<asp:Label ID="Label2" runat="server"
Text="How to Copy Selected Row One
GridView to another GridView"></asp:Label>
</td></tr>
<tr><td><asp:GridView ID="GridView1"
runat="server"
AutoGenerateColumns="False"
>
<Columns>
<asp:TemplateField>
<HeaderTemplate>
<asp:CheckBox ID="chkall"
runat="server"
AutoPostBack="True"
oncheckedchanged="chkall_CheckedChanged" Text="All"
/>
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox ID="chk1"
runat="server"
/>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Id">
<ItemTemplate>
<asp:Label ID="lblid" runat="server"
Text='<%# Eval("id") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Name">
<ItemTemplate>
<asp:Label ID="lblname"
runat="server"
Text='<%# Eval("name") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="RollNo">
<ItemTemplate>
<asp:Label ID="lblrollno"
runat="server"
Text='<%# Eval("rollno") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView></td><td>
<asp:Label ID="Label1" runat="server"
Text="Label"></asp:Label>
</td></tr>
<tr><td>
<asp:Button ID="btnmove"
runat="server"
onclick="btnmove_Click"
Text="GridView Copy" />
</td><td>
</td></tr>
<tr><td>
<asp:Button ID="btncutpaste"
runat="server"
onclick="btncutpaste_Click"
Text="GridView Cut Paste" />
</td><td>
</td></tr>
<tr><td>
<asp:GridView ID="GridView2"
runat="server">
</asp:GridView>
</td><td></td></tr>
</table>
</div>
</form>
</body>
</html>
Source Code for Default.aspx.cs………………………………………..
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
public partial class _Default :
System.Web.UI.Page
{
SqlConnection con;
SqlDataAdapter da;
DataSet ds;
protected void Page_Load(object sender, EventArgs
e)
{
con = new SqlConnection(@"Data Source=KUSH-PC\KUSH;Initial
Catalog=Test;Integrated Security=True");
if (!IsPostBack)
{
BindGridView();
}
}
//Code for bind First GridView with Sql Server table
private void
BindGridView()
{
SqlCommand cmd = new SqlCommand("select
* from Student",con );
da = new SqlDataAdapter(cmd);
ds = new DataSet();
da.Fill(ds);
GridView1.DataSource
= ds;
GridView1.DataBind();
}
//Code for bind Second
GridView with sql table when
private void
bindgridview1()
{
SqlCommand com = new SqlCommand("select
* from Student where id in ("+Label1.Text+")",
con);
SqlDataAdapter da = new SqlDataAdapter(com);
ds = new DataSet();
da.Fill(ds);
GridView2.DataSource
= ds;
GridView2.DataBind();
}
string id;
//Code for How to
Copy data of Gridview1 to another GridiView
protected void
btnmove_Click(object sender, EventArgs e)
{
foreach (GridViewRow
gr in GridView1.Rows)
{
CheckBox ck = (CheckBox)gr.FindControl("chk1");
if (ck.Checked)
{
id +=
((Label)gr.Cells[1].Controls[1]).Text + ",";
}
}
if (id != null)
{
Label1.Text
= id.Remove(id.Length - 1);
}
else
{
Label1.Text
= " 0";
}
bindgridview1();
}
//Code for when cheched Header CheckBox all checkbox is
checked......................
protected void
chkall_CheckedChanged(object sender, EventArgs e)
{
CheckBox ch = (CheckBox)sender;
if (ch.Checked)
{
foreach (GridViewRow
grv in GridView1.Rows)
{
((CheckBox)grv.Cells[0].Controls[1]).Checked = true;
}
}
else
{
foreach (GridViewRow
grv in GridView1.Rows)
{
((CheckBox)grv.Cells[0].Controls[1]).Checked = false;
}
}
}
//Code for How to
cut data of Gridview1 to another GridiView2
protected void
btncutpaste_Click(object sender, EventArgs e)
{
foreach (GridViewRow
gr in GridView1.Rows)
{
CheckBox ck = (CheckBox)gr.FindControl("chk1");
if (ck.Checked)
{
id +=
((Label)gr.Cells[1].Controls[1]).Text + ",";
gr.Visible
= false;
}
}
if (id != null)
{
Label1.Text
= id.Remove(id.Length - 1);
}
else
{
Label1.Text
= " 0";
}
bindgridview1();
}
}
0 comments:
Post a Comment