Hi smile,
Refer below sample.
HTML
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" ShowFooter="true">
<Columns>
<asp:BoundField DataField="ItemCode" HeaderText="Code" />
<asp:BoundField DataField="Item" HeaderText="Item" />
<asp:BoundField DataField="Price" HeaderText="Price" ItemStyle-CssClass="price" />
<asp:TemplateField HeaderText="Quantity">
<ItemTemplate>
<asp:TextBox ID="txtQuantity" runat="server"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Total">
<ItemTemplate>
<asp:Label ID="lblTotal" runat="server" Text="0"></asp:Label>
</ItemTemplate>
<FooterTemplate>
<b>Grand Total: </b>
<asp:Label ID="lblGrandTotal" runat="server" />
</FooterTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<br />
<label class="control-label" style="margin-top: 10px; color: Green; font-size: large;
font-weight: bolder; font-style: oblique;">
Sub Total:</label>
<asp:Label ID="lblGrant" runat="server" Text="0" class="control-label" Style="margin-top: 10px;
color: Red; font-size: large; font-weight: bolder; font-style: italic;"></asp:Label>
<asp:HiddenField ID="hfGrantTotal" runat="server" />
<asp:Button Text="Insert" runat="server" OnClick="Save" />
<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*=txtQuantity]").val("0");
});
$("body").on("change keyup", "[id*=txtQuantity]", function () {
var quantity = parseFloat($.trim($(this).val()));
if (isNaN(quantity)) {
quantity = 0;
}
$(this).val(quantity);
var row = $(this).closest("tr");
$("[id*=lblTotal]", row).html(parseFloat($(".price", row).html()) * parseFloat($(this).val()));
var grandTotal = 0;
$("[id*=lblTotal]").each(function () {
grandTotal = grandTotal + parseFloat($(this).html());
});
$('#GridView1').find('tr:has(td)').find('[id*=lblGrandTotal]')[0].innerHTML = grandTotal;
$("[id*=lblGrant]").html(grandTotal);
$("[id*=hfGrantTotal]").val(grandTotal);
});
</script>
Namespaces
C#
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
VB.Net
Imports System.Data
Imports System.Data.SqlClient
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[] { new DataColumn("ItemCode"), new DataColumn("Item"), new DataColumn("Price") });
dt.Rows.Add(1, "Shirt", 200);
GridView1.DataSource = dt;
GridView1.DataBind();
}
}
protected void Save(object sender, EventArgs e)
{
foreach (GridViewRow row in GridView1.Rows)
{
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand("INSERT INTO SampleTest(ItemCode,Item,Price,GrantTotal) VALUES(@ItemCode,@Item,@Price,@GrantTotal)", con))
{
cmd.Parameters.AddWithValue("@ItemCode", row.Cells[0].Text);
cmd.Parameters.AddWithValue("@Item", row.Cells[1].Text);
cmd.Parameters.AddWithValue("@Price", row.Cells[2].Text);
cmd.Parameters.AddWithValue("@GrantTotal", hfGrantTotal.Value);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}
}
}
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 = New DataTable()
dt.Columns.AddRange(New DataColumn() {New DataColumn("ItemCode"), New DataColumn("Item"), New DataColumn("Price")})
dt.Rows.Add(1, "Shirt", 200)
GridView1.DataSource = dt
GridView1.DataBind()
End If
End Sub
Protected Sub Save(ByVal sender As Object, ByVal e As EventArgs)
For Each row As GridViewRow In GridView1.Rows
Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using con As SqlConnection = New SqlConnection(constr)
Using cmd As SqlCommand = New SqlCommand("INSERT INTO SampleTest(ItemCode,Item,Price,GrantTotal) VALUES(@ItemCode,@Item,@Price,@GrantTotal)", con)
cmd.Parameters.AddWithValue("@ItemCode", row.Cells(0).Text)
cmd.Parameters.AddWithValue("@Item", row.Cells(1).Text)
cmd.Parameters.AddWithValue("@Price", row.Cells(2).Text)
cmd.Parameters.AddWithValue("@GrantTotal", hfGrantTotal.Value)
con.Open()
cmd.ExecuteNonQuery()
con.Close()
End Using
End Using
Next
End Sub
Screenshot