How to sum the value of gridview column using jquery

In this post, I will show how to sum the value of gridview column on the selection of checkbox. Let us suppose that you have a gridview which has three columns named Name, Quantity and Price. Now your requirement is to sum the value of price filed on the selection of checkbox.

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">
<html xmlns="">
<head runat="server">
    <script src="Scripts/jquery-1.4.1.js" type="text/javascript"></script>
    <script type="text/javascript">
        $(document).ready(function () {
    var total;
    //Column index value of price field (Column index start from 1)
    var columnIndexValue = 4;
    var checked = $('input:checkbox').click(function (e) {
    var total = 0.0;
                $("tr:has(:checkbox:checked) td:nth-child(" + columnIndexValue + ")").each(function () {
                    total += parseFloat($(this).text());
                $('#Sum').text("your total is:" + total.toFixed(2));
    <form id="form1" runat="server">
    <asp:GridView ID="grdItems" runat="server" AutoGenerateColumns="false">
    <asp:CheckBox ID="chkItem" runat="server" />
    <asp:TemplateField HeaderText="Name">
    <asp:Label ID="label2" Text='<%#Eval("Name") %>' runat="server" />
    <asp:TemplateField HeaderText="Quantity">
    <asp:Label ID="Label1" Text='<%#Eval("Quantity") %>' runat="server" />
    <asp:TemplateField HeaderText="Price">
    <asp:Label ID="Label2" Text='<%#Eval("Price") %>' runat="server" />
    <div id="Sum">
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class Default2 : System.Web.UI.Page
    protected void Page_Load(object sender, EventArgs e)
        grdItems.DataSource = new Item().Items;
public class Item
    public string Name { get; set; }
    public int Quantity { get; set; }
    public decimal Price { get; set; }
    public List<Item> Items
    return new List<Item>()
    new Item(){Name = "Item01",Quantity = 10,Price = 180M},
    new Item(){Name = "Item01",Quantity = 11,Price = 184M},
    new Item(){Name = "Item01",Quantity = 12,Price = 190M},
    new Item(){Name = "Item01",Quantity = 13,Price = 110M},

Click here for live Demo

Next Post Previous Post
No Comment
Add Comment
comment url