Hi smile,
Check this example. Now please take its reference and correct your code.
HTML
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<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>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript">
$(function () {
$("[id*=txtDiscount]").val("0");
});
$("[id*=txtDiscount]").live("change", function () {
if (isNaN(parseInt($(this).val()))) {
$(this).val('0');
} else {
$(this).val(parseInt($(this).val()).toString());
}
});
$("[id*=txtDiscount]").live("keyup", function () {
var row = $(this).closest("tr");
if (!jQuery.trim($(this).val()) == '') {
if (!isNaN(parseFloat($(this).val()))) {
$("[id*=lblTotal]", row).html(parseFloat($(".amount", row).html()) * parseFloat($(this).val()));
}
} else {
$("[id*=lblTotal]", row).html(0);
}
var grandTotal = 0;
$("#GridView2 [id*=lblTotal]").each(function () {
var total = 0;
if (isNaN($(this).html()) || $(this).html() == "") {
total = 0; ;
} else {
total = $(this).html();
}
grandTotal = grandTotal + parseFloat(total);
});
$("[id*=lblGrandTotal]").html(!isNaN(grandTotal) ? grandTotal.toString() : 0);
});
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" HeaderStyle-BackColor="#3AC0F2" HeaderStyle-ForeColor="White"
Class="table table-striped table-bordered table-hover" runat="server" AutoGenerateColumns="false"
ShowFooter="false">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="cbCheck" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="ItemCode" HeaderText="Code" ItemStyle-Width="200" />
<asp:BoundField DataField="ItemName" HeaderText="Product" ItemStyle-Width="200" />
<asp:BoundField DataField="ItemSize" HeaderText="Weight / Size" ItemStyle-Width="200" />
<asp:BoundField DataField="Qty" HeaderText="Qty" ItemStyle-Width="150" />
<asp:BoundField ItemStyle-Width="150px" DataField="Rate" HeaderText="Price" ItemStyle-CssClass="amount" />
<asp:TemplateField HeaderText="Discount">
<ItemTemplate>
<asp:TextBox ID="txtDiscount" runat="server" Text="0" autocomplete="off" class="field1 form-control"
Width="100"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Total">
<ItemTemplate>
<asp:Label ID="lblTotal" runat="server" Width="100"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<br />
<br />
<asp:GridView ID="GridView2" HeaderStyle-BackColor="#3AC0F2" HeaderStyle-ForeColor="White"
Class="table table-striped table-bordered table-hover" runat="server" AutoGenerateColumns="false">
<EmptyDataTemplate>
<table border="1" cellpadding="0" cellspacing="0">
<tr style="color: White; background-color: #3AC0F2;">
<th scope="col">
Code
</th>
<th scope="col">
Product
</th>
<th scope="col">
Weight/Size
</th>
<th scope="col">
Qty
</th>
<th scope="col">
Price
</th>
<th scope="col">
Count
</th>
<th scope="col">
Total
</th>
</tr>
<tr class="emptyTd">
<td style="width: 30px; text-align: center" colspan="7">
No Records found
</td>
</tr>
</table>
</EmptyDataTemplate>
</asp:GridView>
Grand Total:
<asp:Label ID="lblGrandTotal" runat="server" Text="0"></asp:Label>
</div>
</form>
</body>
</html>
Namespaces
C#
using System.Data;
VB.Net
Imports System.Data
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
DataTable dt = this.GetData();
GridView1.DataSource = dt;
GridView1.DataBind();
GridView2.DataSource = null;
GridView2.DataBind();
}
}
private DataTable GetData()
{
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[] {
new DataColumn("ItemCode", typeof(int)),
new DataColumn("ItemName", typeof(string)),
new DataColumn("ItemSize",typeof(string)),
new DataColumn("Qty",typeof(int)),
new DataColumn("Rate",typeof(int))});
dt.Rows.Add(1, "Item 1", "50", 10, 10);
dt.Rows.Add(2, "Item 2", "50", 10, 10);
dt.Rows.Add(3, "Item 3", "50", 10, 10);
dt.Rows.Add(4, "Item 4", "50", 10, 10);
return dt;
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Dim dt As DataTable = Me.GetData()
GridView1.DataSource = dt
GridView1.DataBind()
GridView2.DataSource = Nothing
GridView2.DataBind()
End If
End Sub
Private Function GetData() As DataTable
Dim dt As DataTable = New DataTable()
dt.Columns.AddRange(New DataColumn() {
New DataColumn("ItemCode", GetType(Integer)),
New DataColumn("ItemName", GetType(String)),
New DataColumn("ItemSize", GetType(String)),
New DataColumn("Qty", GetType(Integer)),
New DataColumn("Rate", GetType(Integer))})
dt.Rows.Add(1, "Item 1", "50", 10, 10)
dt.Rows.Add(2, "Item 2", "50", 10, 10)
dt.Rows.Add(3, "Item 3", "50", 10, 10)
dt.Rows.Add(4, "Item 4", "50", 10, 10)
Return dt
End Function
Screenshot