?
Solved

Gridview  - Footer

Posted on 2007-11-24
2
Medium Priority
?
1,611 Views
Last Modified: 2009-10-13
I have a column call "Open Account". I want to display a total on a footer in Gridview. How can I accomplish this task - VB please.
0
Comment
Question by:VBdotnet2005
[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
2 Comments
 
LVL 12

Expert Comment

by:craskin
ID: 20344581
if you make the open account column a templated column, you can create your own footer template with a label in it, then on the gridview_bound event, do something like

Sub myGridViewDataBound(...) Handles myGridView.DataBound
   Dim Total as Single = 0    
   For each row as GridViewRow in myGridView.Rows
         Total += row.Cells(the integer of Open Account cell).Text
   Next
   CType(myGridView.FindControl("myTotalLabel"), Label).Text = Total
End Sub
           
0
 
LVL 37

Accepted Solution

by:
samtran0331 earned 2000 total points
ID: 20349768
If you declare Total in the rowdatabound, it will reset to zero every time a row binds...also, if you don't specify the rowtype, it will error on the header/footer rows.
Below uses Northwind and gets the numbers 2 different ways and displays them 2 different ways.
<%@ Page Language="VB" %>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
<script runat="server">
    Private TotalUnitPrice As Decimal
    Private TotalQuantity As Integer
    Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs)
        If e.Row.RowType = DataControlRowType.DataRow Then
            'Use dataitem to get data
            TotalUnitPrice += CType(e.Row.DataItem("UnitPrice"), Decimal)
            'use control to get data
            Dim lb As Label
            lb = CType(e.Row.Cells(3).FindControl("lbQuantity"), Label)
            TotalQuantity += CType(lb.Text, Integer)
        ElseIf e.Row.RowType = DataControlRowType.Footer Then
            'write total to the cell
            e.Row.Cells(2).Text = TotalUnitPrice.ToString
            'write total to the label
            CType(e.Row.Cells(3).FindControl("lbQuantityTotal"), Label).Text = TotalQuantity.ToString
        End If
    End Sub
</script>
 
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>Gridview Footer Totals</title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="OrderID,ProductID"
                DataSourceID="SqlDataSource1" EmptyDataText="There are no data records to display."
                OnRowDataBound="GridView1_RowDataBound" ShowFooter="True">
                <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" FooterStyle-HorizontalAlign="right" HeaderStyle-HorizontalAlign="right"
                        HeaderText="UnitPrice" ItemStyle-HorizontalAlign="right" SortExpression="UnitPrice" />
                    <asp:TemplateField FooterStyle-HorizontalAlign="right" HeaderStyle-HorizontalAlign="right"
                        HeaderText="Quantity" ItemStyle-HorizontalAlign="right" SortExpression="Quantity">
                        <ItemTemplate>
                            <asp:Label ID="lbQuantity" runat="server" Text='<%# Eval("Quantity") %>'></asp:Label>
                        </ItemTemplate>
                        <FooterTemplate>
                            <asp:Label ID="lbQuantityTotal" runat="server" Text=""></asp:Label>
                        </FooterTemplate>
                    </asp:TemplateField>
                </Columns>
            </asp:GridView>
            <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"
                ProviderName="<%$ ConnectionStrings:NorthwindConnectionString.ProviderName %>"
                SelectCommand="SELECT TOP 15 [OrderID], [ProductID], [UnitPrice], [Quantity], [Discount] FROM [Order Details]">
            </asp:SqlDataSource>
        </div>
    </form>
</body>
</html>

Open in new window

0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

This article discusses the ASP.NET AJAX ModalPopupExtender control. In this article we will show how to use the ModalPopupExtender control, how to display/show/call the ASP.NET AJAX ModalPopupExtender control from javascript, how to show/display/cal…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

764 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