Solved

How to display gridview summary footer

Posted on 2008-06-17
11
1,489 Views
Last Modified: 2013-11-26
Hi,
I am trying to display summary total of the 'Price' column in a gridview footer, I have tried to follow the example on www.asp.net turorials section but cannot seem to get this to work with my database. The page loads without any errors and displays the grid with data but does not display any totals in the footer.

Here is the code I am using;

Sql;
SELECT        Date, Purchase_Order_number, Job_Number, Price, Invoiced, Paid
FROM            dbo.Purchase_Orders
WHERE        (Paid = 0)

GridView;
                    <asp:GridView ID="Postotal" runat="server" AutoGenerateColumns="False"
                        DataKeyNames="Purchase_Order_number" DataSourceID="ObjectDataSource1"
                        ShowFooter="True">
                        <Columns>
                            <asp:BoundField DataField="Date" HeaderText="Date" SortExpression="Date" />
                            <asp:BoundField DataField="Purchase_Order_number"
                                HeaderText="Purchase_Order_number" InsertVisible="False" ReadOnly="True"
                                SortExpression="Purchase_Order_number" />
                            <asp:BoundField DataField="Job_Number" HeaderText="Job_Number"
                                SortExpression="Job_Number" />
                            <asp:BoundField DataField="Price" HeaderText="Price" SortExpression="Price" />
                            <asp:CheckBoxField DataField="Invoiced" HeaderText="Invoiced"
                                SortExpression="Invoiced" />
                            <asp:CheckBoxField DataField="Paid" HeaderText="Paid" SortExpression="Paid" />
                        </Columns>
                    </asp:GridView>
                    <asp:ObjectDataSource ID="ObjectDataSource1" runat="server"
                        DeleteMethod="Delete" InsertMethod="Insert"
                        OldValuesParameterFormatString="original_{0}" SelectMethod="GetPos"
                        TypeName="accountsTableAdapters.Purchase_OrdersTableAdapter"
                        UpdateMethod="Update">
Code Behind;
public partial class CustomFormatting_SummaryDataInFooter : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    // Class-scope, running total variables...
    decimal _totalPrice = 0m;
    int _totalNonNullPriceCount = 0;

    protected void Postotal_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            // Reference the ProductsRow via the e.Row.DataItem property
            accounts.Purchase_OrdersRow product = (accounts.Purchase_OrdersRow)((System.Data.DataRowView)e.Row.DataItem).Row;

            // Increment the running totals (if they're not NULL!)
            if (!product.IsPriceNull())
            {
                _totalPrice += product.Price;
                _totalNonNullPriceCount++;
            }
        }
        else if (e.Row.RowType == DataControlRowType.Footer)
        {
            // Display the summary data in the appropriate cells
            e.Row.Cells[1].Text = "Total.: " + _totalPrice.ToString();
        }
    }

}

Thanks


Chris
0
Comment
Question by:chrispaton
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 5
11 Comments
 
LVL 12

Expert Comment

by:jandromeda
ID: 21800937
Have you assigned the RowDataBound event handler to the GridView's RowDataBound event?
0
 
LVL 12

Expert Comment

by:jandromeda
ID: 21800986
Replace your Grid View definition with this.
<asp:GridView ID="Postotal" runat="server" AutoGenerateColumns="False"
                        DataKeyNames="Purchase_Order_number" DataSourceID="ObjectDataSource1"
                        ShowFooter="True" onrowdatabound="Postotal_RowDataBound_RowDataBound">

Open in new window

0
 

Author Comment

by:chrispaton
ID: 21801040
I have tried changing the Gridview as suggested but get a compilation error;
does not contain a definition for 'Postotal_RowDataBound_RowDataBound'

I have tried chainging the onrowdatabound to onrowdatabound="Postotal_RowDataBound" but still get error does not contain a definition for 'Postotal_RowDataBound'
0
Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

 
LVL 12

Expert Comment

by:jandromeda
ID: 21801072
Oops! my mistake. But you have corrected it. What is the compiler error message you get now? I have included the corrected code.
<asp:GridView ID="Postotal" runat="server" AutoGenerateColumns="False"
                        DataKeyNames="Purchase_Order_number" DataSourceID="ObjectDataSource1"
                        ShowFooter="True" onrowdatabound="Postotal_RowDataBound">

Open in new window

0
 

Author Comment

by:chrispaton
ID: 21801142
This is the error message;

Compiler Error Message: CS0117: 'ASP.pos_totals_aspx' does not contain a definition for 'Postotal_RowDataBound'

but the Postotal_RowDataBound' does exist in the code.
0
 
LVL 12

Expert Comment

by:jandromeda
ID: 21801165
Can you post the aspx page code and code behind code? If you do not like to post the full code, just the @Page directive of the aspx page and the class definition of the code behind is enough.
0
 

Author Comment

by:chrispaton
ID: 21801199
Here is the code;

I am using Visual Studio 2008 and .net 2.0
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="pos_totals.aspx.cs" Inherits="pos_totals" %>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title>Logs-Purchase Orders</title>
    <link href="includes/StyleSheet.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div align="left">
<table width="100%" class="top_bar" cellpadding="5" cellspacing="0">
    <tr>
        <td>
            <img src="images/logo.jpg" />   
        </td>
    </tr>
</table>
</div>
     <form id="form1" runat="server">
<div align="left">
<table class="navigation_bar">
    <tr>
        <td>
        <asp:HyperLink ID="HyperLink6" runat="server" NavigateUrl="http://companyweb/default.aspx" class="link-navig">Intranet Home</asp:HyperLink>&nbsp &nbsp &nbsp &nbsp &nbsp   
        <asp:HyperLink ID="HyperLink2" runat="server" NavigateUrl="~/default.aspx" class="link-navig">Home</asp:HyperLink>&nbsp &nbsp &nbsp &nbsp &nbsp   
        </td>
        <td align="right">
        <strong>Accounts</strong>
        </td>
    </tr>
</table>
</div>
<div>
   <table width="100%">
        <tr valign="top">
            <td width="50" align="left">    
                <!-- #Include File="includes/tree_navigation.inc" -->
            </td>
            <td>
                <div align="center">
                    <h3>POs to be Paid</h3>
                                        <br />
                    &nbsp;<br />
                    &nbsp; &nbsp;&nbsp;
 
                        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
                        DataKeyNames="Purchase_Order_number" DataSourceID="ObjectDataSource1"
                        ShowFooter="True" onrowdatabound="Postotal_RowDataBound">
                        <Columns>
                            <asp:BoundField DataField="Date" HeaderText="Date" SortExpression="Date" />
                            <asp:BoundField DataField="Purchase_Order_number" 
                                HeaderText="Purchase_Order_number" InsertVisible="False" ReadOnly="True" 
                                SortExpression="Purchase_Order_number" />
                            <asp:BoundField DataField="Job_Number" HeaderText="Job_Number" 
                                SortExpression="Job_Number" />
                            <asp:BoundField DataField="Price" HeaderText="Price" SortExpression="Price" />
                            <asp:CheckBoxField DataField="Invoiced" HeaderText="Invoiced" 
                                SortExpression="Invoiced" />
                            <asp:CheckBoxField DataField="Paid" HeaderText="Paid" SortExpression="Paid" />
                        </Columns>
                    </asp:GridView>
                    <asp:ObjectDataSource ID="ObjectDataSource1" runat="server" 
                        DeleteMethod="Delete" InsertMethod="Insert" 
                        OldValuesParameterFormatString="original_{0}" SelectMethod="GetPos" 
                        TypeName="accountsTableAdapters.Purchase_OrdersTableAdapter" 
                        UpdateMethod="Update">
                        <DeleteParameters>
                            <asp:Parameter Name="Original_Purchase_Order_number" Type="Int32" />
                        </DeleteParameters>
                        <UpdateParameters>
                            <asp:Parameter Name="Date" Type="DateTime" />
                            <asp:Parameter Name="Job_Number" Type="Int32" />
                            <asp:Parameter Name="Price" Type="Decimal" />
                            <asp:Parameter Name="Invoiced" Type="Boolean" />
                            <asp:Parameter Name="Paid" Type="Boolean" />
                            <asp:Parameter Name="Original_Purchase_Order_number" Type="Int32" />
                        </UpdateParameters>
                        <InsertParameters>
                            <asp:Parameter Name="Date" Type="DateTime" />
                            <asp:Parameter Name="Job_Number" Type="Int32" />
                            <asp:Parameter Name="Price" Type="Decimal" />
                            <asp:Parameter Name="Invoiced" Type="Boolean" />
                            <asp:Parameter Name="Paid" Type="Boolean" />
                        </InsertParameters>
                    </asp:ObjectDataSource>
                       </div>
            </td>
        </tr>
    </table>
</div>   
 
    </form>
 
</body>
</html>
 
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
 
public partial class pos_totals : System.Web.UI.Page
{
}
 
public partial class CustomFormatting_SummaryDataInFooter : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
 
    }
 
    // Class-scope, running total variables...
    decimal _totalPrice = 0m;
    int _totalNonNullPriceCount = 0;
 
    protected void Postotal_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            // Reference the ProductsRow via the e.Row.DataItem property
            accounts.Purchase_OrdersRow product = (accounts.Purchase_OrdersRow)((System.Data.DataRowView)e.Row.DataItem).Row;
 
            // Increment the running totals (if they're not NULL!)
            if (!product.IsPriceNull())
            {
                _totalPrice += product.Price;
                _totalNonNullPriceCount++;
            }
        }
        else if (e.Row.RowType == DataControlRowType.Footer)
        {
            // Display the summary data in the appropriate cells
            e.Row.Cells[1].Text = "Total.: " + _totalPrice.ToString();
        }
    }
 
}

Open in new window

0
 
LVL 12

Accepted Solution

by:
jandromeda earned 500 total points
ID: 21801322
The Postotal_RowDataBound event is in the CustomFormatting_SummaryDataInFooter class not in the pos_totals class. Add that event to the pos_totals class. pos_totals is the code behind of your aspx page not CustomFormatting_SummaryDataInFooter class.
0
 

Author Comment

by:chrispaton
ID: 21801366
Perfect, it works!!

Thanks for your help jandromeda

Chris
0
 

Author Closing Comment

by:chrispaton
ID: 31467886
Thanks for your help, Chris
0
 
LVL 12

Expert Comment

by:jandromeda
ID: 21801550
You are welcome Chris. Happy coding! :)
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question