Hello everyone
I have 2 gridview and both gridview have a common column "Select" through which I am selecting a particular row.
When I select a row in first grid the clone row created and append to 2nd grid using jquery.
Now the problem is that when I select the row in second grid (which is the clone row of first grid) the same action performs.
I can understand that why it is happening, because it is the clone of first grid, but I want to prevent from this situation.
I want that the action performed by my jquery code happens only when I select row from first Grid
Please help.
Here is my code
Jquery
<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 temprow = $('[id*=GridView2] table tbody').find(".emptyTd");
$('[id*=cbCheck]').change(function () {
var checkbox = $(this);
if ($(this).is(":checked")) {
var row = $(this).parent().closest('tr');
var temp = $(row).clone(true);
$('[id*=GridView1] tbody').append(temp);
$('[id*=GridView2] table tbody').find(".emptyTd").remove();
//$(row).find("td:first").remove();
$('[id*=GridView2] table tbody').append(row);
}
else {
$('[id*=GridView2] table tbody tr').each(function () {
if ($(this).find("td:first").html() == $(checkbox).parent().closest('tr').find("td:nth-child(2)").html()) {
$(this).remove();
if ($('[id*=GridView2] table tbody').has('td').length == 0) {
$('[id*=GridView2] table tbody').append(temprow);
}
}
});
}
});
});
</script>
HTML
<div>
<asp:GridView ID="GridView1" HeaderStyle-BackColor="#3AC0F2" HeaderStyle-ForeColor="White"
runat="server" AutoGenerateColumns="false" Width="330">
<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>
<br />
<br />
<asp:GridView ID="GridView2" HeaderStyle-BackColor="#3AC0F2" HeaderStyle-ForeColor="White"
runat="server" AutoGenerateColumns="False">
<EmptyDataTemplate>
<table border="0" cellpadding="0" cellspacing="0" width="325px">
<tr style="color: White; background-color: #3AC0F2;">
<th scope="col">
Select
</th>
<th scope="col">
Id
</th>
<th scope="col">
Name
</th>
<th scope="col">
Country
</th>
</tr>
<tr class="emptyTd">
<td style="width: 30px; text-align: center" colspan="3">
No Records found
</td>
</tr>
</table>
</EmptyDataTemplate>
<HeaderStyle BackColor="#3AC0F2" ForeColor="White"></HeaderStyle>
</asp:GridView>
</div>
C#
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();
GridView2.DataSource = null;
GridView2.DataBind();
}