This article shows how to get a GridView's current page row column total of a sum and the GridView's all rows column grand total of the sum.

Step 1: Database

First create a table in a database as in the following.

Table: Item

  1. create table Item
  2. (
  3. Itemid int identity(1, 1) primary key,
  4. ItemName varchar(100),
  5. ItemPrice decimal(7, 2)
  6. )
Now fill in some records in a database as in the following:



Figure 1: Table Record

Step 2: Visual Studio
  1. Add a new project.
  2. Go to the project in Solution Explorer.
  3. Right-click and Add Item.
  4. Select Web Form as in the following.



Figure 2: Add Form

Step 3: UI Design

In this Design section add a GridView control to display the database table data on a web page.

UI Design

  1. <%@ Page Title="" Language="C#" MasterPageFile="~/Master/Master.master" AutoEventWireup="true" CodeFile="Gridviewsum.aspx.cs" Inherits="UI_Gridviewsum" %>
  2. <asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
  3. </asp:Content>
  4. <asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
  5. <h2 style="color: #00CC00">Gridview Column Total and Sub Total</h2>
  6. <div>
  7. <asp:GridView ID="GridItem" runat="server" Width="100%"
  8. AutoGenerateColumns="False"
  9. RowStyle-HorizontalAlign="Center"
  10. ShowFooter="True" AllowPaging="True" PageSize="5" OnPageIndexChanging="GridItem_PageIndexChanging" OnRowDataBound="GridItem_RowDataBound"
  11. BackColor ="White" BorderColor="#CC9966" BorderWidth="1px" CellPadding="4" >
  12. <Columns>
  13. <asp:TemplateField HeaderText="Item No">
  14. <ItemTemplate><%#Eval("Itemid")%></ItemTemplate>
  15. </asp:TemplateField>
  16. <asp:TemplateField HeaderText="Item Name">
  17. <ItemTemplate><%#Eval("ItemName")%></ItemTemplate>
  18. <FooterTemplate>
  19. <div style="padding:0 0 5px 0"><asp:Label Text="Page Total" runat="server" /></div>
  20. <div><asp:Label Text="Grand Total" runat="server" /></div>
  21. </FooterTemplate>
  22. </asp:TemplateField>
  23. <asp:TemplateField HeaderText="Price ">
  24. <ItemTemplate><asp:Label ID="lblPrice" runat="server" Text='<%#Eval("ItemPrice")%>'>
  25. </asp:Label></ItemTemplate>
  26. <FooterTemplate>
  27. <div style="padding:0 0 5px 0"><asp:Label ID="lblTotal" runat="server" /></div>
  28. <div><asp:Label ID="lblTotalPrice" runat="server" /></div>
  29. </FooterTemplate>
  30. </asp:TemplateField>
  31. </Columns>
  32. <FooterStyle BackColor="#FFFFCC" ForeColor="#330099" />
  33. <HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="#FFFFCC" />
  34. <PagerStyle BackColor="#FFFFCC" ForeColor="#330099" HorizontalAlign="Center" />
  35. <RowStyle BackColor="White" ForeColor="#330099" />
  36. <SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="#663399" />
  37. <SortedAscendingCellStyle BackColor="#FEFCEB" />
  38. <SortedAscendingHeaderStyle BackColor="#AF0101" />
  39. <SortedDescendingCellStyle BackColor="#F6F0C0" />
  40. <SortedDescendingHeaderStyle BackColor="#7E0000" />
  41. </asp:GridView>
  42. </div>
  43. </asp:Content>
Step 4: UI Code

In this code section we will write the GridView bind code and add a GridView page index changing command and row data bound command as in the following from the GridView properties.



Figure 3: Add Command

Also add a viewstate for the store item price.

UI CODE
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.UI;
  6. using System.Web.UI.WebControls;
  7. using System.Configuration;
  8. using System.Data;
  9. using System.Data.SqlClient;
  10. public partial class UI_Gridviewsum: System.Web.UI.Page {
  11. string connection = ConfigurationManager.ConnectionStrings["connstring"].ConnectionString;
  12. protected void Page_Load(object sender, EventArgs e) {
  13. if (!IsPostBack) {
  14. BindItemList();
  15. }
  16. }
  17. private void BindItemList() {
  18. string Query = "SELECT * FROM Item";
  19. using(SqlConnection con = new SqlConnection(connection)) {
  20. using(SqlCommand cmd = con.CreateCommand()) {
  21. cmd.CommandText = Query;
  22. con.Open();
  23. DataTable dt = new DataTable();
  24. SqlDataAdapter da = new SqlDataAdapter();
  25. da.SelectCommand = cmd;
  26. da.Fill(dt);
  27. if (ViewState["TotalPrice"] == null) {
  28. Decimal Price = 0;
  29. for (int i = 0; i <= dt.Rows.Count - 1; i++) {
  30. Price += dt.Rows[i].Field < Decimal > (2);
  31. }
  32. ViewState["TotalPrice"] = Price;
  33. }
  34. GridItem.DataSource = dt;
  35. GridItem.DataBind();
  36. }
  37. }
  38. }
  39. protected void GridItem_PageIndexChanging(object sender, GridViewPageEventArgs e) {
  40. GridItem.PageIndex = e.NewPageIndex;
  41. BindItemList();
  42. }
  43. Decimal Page_Sum;
  44. protected void GridItem_RowDataBound(object sender, GridViewRowEventArgs e) {
  45. if (e.Row.RowType == DataControlRowType.DataRow) {
  46. Label lblPageTotal = (Label) e.Row.FindControl("lblPrice");
  47. Page_Sum += Decimal.Parse(lblPageTotal.Text);
  48. }
  49. if (e.Row.RowType == DataControlRowType.Footer) {
  50. if (ViewState["TotalPrice"] != null && Page_Sum != 0) {
  51. Label lblTotal = (Label) e.Row.FindControl("lblTotal");
  52. lblTotal.Text = Page_Sum.ToString();
  53. Label lblTotalPrice = (Label) e.Row.FindControl("lblTotalPrice");
  54. lblTotalPrice.Text = ViewState["TotalPrice"].ToString();
  55. }
  56. }
  57. }
  58. }
Step 5: Browser

Now your page is ready to run in a browser, press F5.



Figure 4: Grid Page 1 Sub Total and Grand Total



Figure 5: Grid Page 2 Sub Total and Grand Total

I hope you understand how to get a running item price subtotal and total using an ASP.Net GridView control.