Now here's an example and it also uses jQuery.
HTML
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="cbCheck" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="Id" HeaderText="Id" ItemStyle-Width="30" />
<asp:BoundField DataField="Name" HeaderText="Name" ItemStyle-Width="150" />
<asp:BoundField DataField="Country" HeaderText="Country" ItemStyle-Width="150" />
</Columns>
</asp:GridView>
<hr />
<asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="Id" HeaderText="Id" ItemStyle-Width="30" />
<asp:BoundField DataField="Name" HeaderText="Name" ItemStyle-Width="150" />
<asp:BoundField DataField="Country" HeaderText="Country" ItemStyle-Width="150" />
</Columns>
<EmptyDataTemplate>
<table border="0" cellpadding="0" cellspacing="0" width="330px">
<tr>
<th scope="col">
Id
</th>
<th scope="col">
Name
</th>
<th scope="col">
Country
</th>
</tr>
<tr class = "trEmpty">
<td style="width: 30px; text-align: center" colspan="3">
No Records found
</td>
</tr>
</table>
</EmptyDataTemplate>
</asp:GridView>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script type="text/javascript">
$(function () {
var empty = $("[id*=GridView2] .trEmpty").clone();
$('[id*=cbCheck]').change(function () {
var checkbox = $(this);
if ($("[id*=GridView2] .trEmpty").length > 0) {
$("[id*=GridView2] .trEmpty").remove();
}
if ($(this).is(":checked")) {
var row = $(this).closest('tr').clone(true);
row.find("td:first").remove();
$('[id*=GridView2] > tbody').append(row);
}
else {
$('[id*=GridView2] tr').each(function () {
if ($(this).find("td:first").html() == checkbox.closest('tr').find("td:nth-child(2)").html()) {
var temp = $(this).clone();
$(this).remove();
$('[id*=GridView1] table tbody').append(temp);
}
});
}
});
});
</script>
Namespaces
using System.Data;
Code
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[3] { new DataColumn("Id", typeof(int)),
new DataColumn("Name", typeof(string)),
new DataColumn("Country",typeof(string)) });
dt.Rows.Add(1, "John Hammond", "United States");
dt.Rows.Add(2, "Mudassar Khan", "India");
dt.Rows.Add(3, "Suzanne Mathews", "France");
dt.Rows.Add(4, "Robert Schidner", "Russia");
GridView1.DataSource = dt;
GridView1.DataBind();
DataTable dt2 = new DataTable();
dt2.Columns.AddRange(new DataColumn[3] { new DataColumn("Id", typeof(int)),
new DataColumn("Name", typeof(string)),
new DataColumn("Country",typeof(string)) });
dt2.Rows.Add(5, "Shen Chin", "China");
dt2.Rows.Add(6, "Will Smith", "Peru");
GridView2.DataSource = dt2;
GridView2.DataBind();
}
}