Calculate Row Total and Grand Total in ASP.Net GridView using jQuery
 
Author:
Filed Under: ASP.Net  |  GridView  |  JQuery
Published Date: Dec 28, 2011
Views: 1135
 

Abstract: Here Mudassar Ahmed Khan has explained how to calculate per row total for each item depending on quanitity and the final grand total in the footer of ASP.Net GridView using jQuery

Comments:  1

 

In this article I will explain how to calculate row total and grand total in ASP.Net GridView using jQuery.
 
GridView Markup
Below is the markup of the ASP.Net GridView. It has 2 Bound Fields one for Item and other for Price, one ASP.Net TextBox to enter the quantity of the item and finally an ASP.Net Label control to display the Item Total which is Quantity * Price
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
    <Columns>
        <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>
        </asp:TemplateField>
    </Columns>
</asp:GridView>
Grand Total:
<asp:Label ID="lblGrandTotal" runat="server" Text="0"></asp:Label>
 
 
jQuery Calculation
Below is the jQuery code snippet that does all the calculations. I have added necessary checks so that invalid inputs can be restricted.
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script type="text/javascript">
    $(function () {
        $("[id*=txtQuantity]").val("0");
    });
    $("[id*=txtQuantity]").live("change", function () {
        if (isNaN(parseInt($(this).val()))) {
           $(this).val('0');
        } else {
           $(this).val(parseInt($(this).val()).toString());
        }
    });
    $("[id*=txtQuantity]").live("keyup", function () {
        if (!jQuery.trim($(this).val()) == '') {
            if (!isNaN(parseFloat($(this).val()))) {
                var row = $(this).closest("tr");
                $("[id*=lblTotal]", row).html(parseFloat($(".price", row).html()) * parseFloat($(this).val()));
            }
        } else {
            $(this).val('');
        }
        var grandTotal = 0;
        $("[id*=lblTotal]").each(function () {
            grandTotal = grandTotal + parseFloat($(this).html());
       });
        $("[id*=lblGrandTotal]").html(grandTotal.toString());
    });
</script>
 
Demo
Item Price Quantity Total
Shirt 200 0
Football 30 0
Bat 22.5 0
Grand Total: 0
Downloads
You can download the complete source code using the download link provided below
GridViewRowandGrandTotaljQuery.zip

 









Related Articles



Comments



Add comments

You can add your comment about this article using the form below. Make sure you provide a valid email address
else you won't be notified when the author replies to your comment

Please note that all comments are moderated and will be deleted if they are
  • Not relavant to the article
  • Spam
  • Advertising campaigns or links to other sites
  • Abusive content.
Please do not post code, scripts or snippets.

Name*: Required
Email*: Required
Comment*: Required
Security code*: CaptchaInvalid Security Code
  Submit