Click here to Skip to main content
15,881,757 members
Articles / Web Development / ASP.NET

Summing Columns in a GridView

Rate me:
Please Sign up or sign in to vote.
3.00/5 (2 votes)
8 Apr 2009CPOL 23.4K   8   2
Level: Beginner. After getting information from a database, you may also want summary information about the data (totals, averages, counts, etc.). This can be done using SQL group by clauses or, it can be done while binding to a GridView.

Level: Beginner.

After getting information from a database, you may also want summary information about the data (totals, averages, counts, etc.). This can be done using SQL group by clauses or, it can be done while binding to a GridView. I've seen other examples of this but they seem more complicated than they need to be.

Note: If the GridView has more than one page, only the data on the current page can be summarized using this technique.

Note: The sample uses the OrderDetails table from the NorthWind database.

ASP code:

ASP.NET
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
    DataKeyNames="OrderID,ProductID" DataSourceID="SqlDataSource1" 
    ondatabinding="GridView1_DataBinding" 
    ondatabound="GridView1_DataBound" 
    onrowdatabound="GridView1_RowDataBound">
      <Columns>
          <asp:BoundField DataField="OrderID" HeaderText="OrderID" ReadOnly="True" 
              SortExpression="OrderID" />
          <asp:BoundField DataField="ProductID" HeaderText="ProductID" ReadOnly="True" 
              SortExpression="ProductID" />
          <asp:BoundField DataField="UnitPrice" HeaderText="UnitPrice" 
              SortExpression="UnitPrice" />
          <asp:BoundField DataField="Quantity" HeaderText="Quantity" 
              SortExpression="Quantity" />
          <asp:BoundField DataField="Discount" HeaderText="Discount" 
              SortExpression="Discount" />
      </Columns>
</asp:GridView>

C# Code:

C#
private Decimal OrderTotal;  // holds total price of order
// ---- the grid is about to start getting data...initialize
protected void GridView1_DataBinding(object sender, EventArgs e)
{
    OrderTotal = 0.0M;
}

// ---- Each row is data bound ------------------------------

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        DataRowView DRV = e.Row.DataItem as DataRowView;

        // price is in column 2
        // quantity is in column 3
        Decimal Price = (Decimal)DRV.Row.ItemArray[2];
        short Quantity = (short)DRV.Row.ItemArray[3];

        Decimal RowTotal = Price * Quantity;

        OrderTotal += RowTotal;
    }
}

// ---- The grid is done getting data, show the Order Total --------

protected void GridView1_DataBound(object sender, EventArgs e)
{
    LabelOrderTotal.Text = OrderTotal.ToString("C");
}

I hope you find this useful.

Steve Wellens

Copyright 2008 Steve Wellens

This article was originally posted at http://weblogs.asp.net/stevewellens/privaterss.aspx

License

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


Written By
EndWell Software, Inc.
United States United States
I am an independent contractor/consultant working in the Twin Cities area in Minnesota. I work in .Net, Asp.Net, C#, C++, XML, SQL, Windows Forms, HTML, CSS, etc., etc., etc.

Comments and Discussions

 
GeneralMy vote of 2 Pin
KU4-Dec-09 20:11
KU4-Dec-09 20:11 
GeneralNice and clean Pin
Sean J Seymour24-Apr-09 1:40
Sean J Seymour24-Apr-09 1:40 

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.