Click here to Skip to main content
15,888,202 members
Articles / Programming Languages / C#
Article

This article explains how to display totals in the GridView.

Rate me:
Please Sign up or sign in to vote.
3.45/5 (5 votes)
11 May 2008CPOL2 min read 25.7K   236   20   3
This article explains how to display totals in the GridView.
GridViewTotals

Introduction

This article explains how to display totals in the GridView.

The Code

Create a new ASP.NET page and drag a GridView on it. Enable the GridView Footer by setting the ShowFooter property to True on the Properties pane.
Now, we will write the code to populate the GridView with data from the database. We will be using the Northwind table of Sql Server 2000. The query to populate the GridView is as
SELECT ProductName,UnitPrice FROM Products WHERE UnitsInStock >100 ORDER BY ProductName
Means, we will be displaying the ProductName and UnitPrice for those products where the UnitsInStock are more than 100 from the Products table of the Northwind database.

In the Page_Load() event, write the code to populate the GridView as:-
SqlConnection con = new SqlConnection("Data Source=localhost;Initial Catalog=Northwind;Integrated Security=True");
SqlDataAdapter da = new SqlDataAdapter("SELECT ProductName,UnitPrice FROM Products WHERE UnitsInStock >100 ORDER BY ProductName", con);
DataSet ds = new DataSet();
da.Fill(ds, "Products");

GridView1.DataSource = ds;
GridView1.DataBind();
In the above code, the DataSet stores the data from the select query of the SqlDataAdapter. This DataSet is used as the DataSource for the GridView. Finally, the DataBind() method is called for the GridView to bind the GridView with the data.

Now, insert an event handler for the RowCreated event into your code behind file. Then check the RowType of each new row. In case of DataControlRowType.DataRow add the value of UnitPrice to the variable tp which is a global variable for the Page with a decimal data type. In case of DataControlRowType.Footer assign the value of tp to the first cell of the GridView footer.

if (e.Row.RowType == DataControlRowType.DataRow)
        {
            tp += Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, "UnitPrice"));
        }
        if (e.Row.RowType == DataControlRowType.Footer)
        {
            e.Row.Cells[0].Text = "Total Unit Price : ";
            e.Row.Cells[1].Text = tp.ToString("c");
            e.Row.Cells[0].HorizontalAlign = HorizontalAlign.Right;
            e.Row.Cells[1].HorizontalAlign = HorizontalAlign.Right;
            e.Row.Font.Bold = true;

        }
The complete code for the code behind of the page is as

using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Products : System.Web.UI.Page
{
    Decimal tp = 0;

    protected void Page_Load(object sender, EventArgs e)
    {
            SqlConnection con = new SqlConnection("Data Source=localhost;Initial Catalog=Northwind;Integrated Security=True");
            SqlDataAdapter da = new SqlDataAdapter("SELECT ProductName,UnitPrice FROM Products WHERE UnitsInStock >100 ORDER BY ProductName", con);
            DataSet ds = new DataSet();
            da.Fill(ds, "Products");

            GridView1.DataSource = ds;
            GridView1.DataBind();
    }
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            tp += Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, "UnitPrice"));
        }
        if (e.Row.RowType == DataControlRowType.Footer)
        {
            e.Row.Cells[0].Text = "Total Unit Price : ";
            e.Row.Cells[1].Text = tp.ToString("c");
            e.Row.Cells[0].HorizontalAlign = HorizontalAlign.Right;
            e.Row.Cells[1].HorizontalAlign = HorizontalAlign.Right;
            e.Row.Font.Bold = true;

        }
    }
}
The RowDataBound event is raised whenever a row in the GridView is bound to data.

This event provides an opportunity to access each row before the page is finally sent to the client for display. After this event is raised, the data item is nulled out and no longer available.

This event is raised for the header, the footer, and data rows.

Information related to the RowDataBound event is passed via a GridViewRowEventArgs object to the method assigned to handle the event. The following GridViewRowEventArgs property provides information specific to this event.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
India India
Sudipta Chaudhari is having 14+ yrs of professional full stack software design & development experience with multiple onsite/client site visit experiences.

He has extensive experience working on – REACT, C#, .NET Core, Angular, AZURE, ASP.NET Web API, ASP.NET MVC, WPF, SQL Server, JQuery to name a few. He also has experience as a SCRUM Master.

For latest articles and updates, please visit by website/blog : http://sudiptachaudhari.com

Comments and Discussions

 
GeneralSimple way to do Pin
techbrij24-Dec-11 2:45
techbrij24-Dec-11 2:45 
GeneralRe: Simple way to do Pin
Sudipta Chaudhari24-Dec-11 17:33
Sudipta Chaudhari24-Dec-11 17:33 
GeneralNice copied artice :) Pin
KamranShahid11-May-08 21:48
KamranShahid11-May-08 21:48 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.