Link to home
Start Free TrialLog in
Avatar of dba123
dba123

asked on

Add Grand Total Row at bottom of GridView

How can I add a last row in my Gridview that shows a Grand Total of the 2 Budget columns?

        <asp:GridView
         ID="gv_dashboard"
         runat="server"
         AutoGenerateColumns="False"
         DataSourceID="ds_dashboard"
         CellPadding="4"
         ForeColor="#333333"
         ShowFooter="True"  
         GridLines="None"
         CssClass="FormatFont">
     
            <Columns>
                <asp:TemplateField HeaderText="Cust #" SortExpression="CustomerID">
                <HeaderStyle HorizontalAlign="center"></HeaderStyle>
                    <ItemTemplate>
                        <asp:Label runat="server" id="lblCustomerID" Text='<%# Bind("Customer") %>' />
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="AZ Budget" SortExpression="Name" ItemStyle-HorizontalAlign="Center">
                  <HeaderStyle HorizontalAlign="Left"></HeaderStyle>
                     <ItemTemplate>
                        <asp:TextBox width="55px"  MaxLength="10" ID="txtAZBudget" Text='<%# Bind("AZBudget") %>' runat="server" />
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="IL Budget" SortExpression="Name" ItemStyle-HorizontalAlign="Center">
                  <HeaderStyle HorizontalAlign="Left"></HeaderStyle>
                     <ItemTemplate>
                        <asp:TextBox width="55px"  MaxLength="10" ID="txtILBudget" Text='<%# Bind("ILBudget") %>' runat="server" />
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="AZ" SortExpression="Name" ItemStyle-HorizontalAlign="Center">
                 <HeaderStyle HorizontalAlign="center"></HeaderStyle>
                     <ItemTemplate>
                        <asp:checkbox runat="server" Id="chbx_Arizona" Checked='<%# Bind("Arizona") %>'/>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="IL" SortExpression="Name" ItemStyle-HorizontalAlign="Center">
                 <HeaderStyle HorizontalAlign="center"></HeaderStyle>
                     <ItemTemplate>
                        <asp:checkbox runat="server" Id="chbx_Illinois" Checked='<%# Bind("Illinois") %>'/>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Current Month Collections" SortExpression="Name" ItemStyle-HorizontalAlign="Center">
                 <HeaderStyle HorizontalAlign="center"></HeaderStyle>
                     <ItemTemplate>
                        <asp:checkbox runat="server" Id="chbx_CurrentMonthCollections" Checked='<%# Bind("CurrentMonthCollections") %>'/>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Revenue By Month" SortExpression="Name" ItemStyle-HorizontalAlign="Center">
                 <HeaderStyle HorizontalAlign="center"></HeaderStyle>
                     <ItemTemplate>
                        <asp:checkbox runat="server" Id="chbx_RevenueByMonth" Checked='<%# Bind("RevenueByMonth") %>'/>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="PDCs & CCS Monthly" SortExpression="Name" ItemStyle-HorizontalAlign="Center">
                 <HeaderStyle HorizontalAlign="center"></HeaderStyle>
                     <ItemTemplate>
                        <asp:checkbox runat="server" Id="chbx_PDCsCCsMonthly" Checked='<%# Bind("PDCsCCsMonthly") %>'/>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Revenue By Client" SortExpression="Name" ItemStyle-HorizontalAlign="Center">
                 <HeaderStyle HorizontalAlign="center"></HeaderStyle>
                     <ItemTemplate>
                        <asp:checkbox runat="server" Id="chbx_RevenueByClient" Checked='<%# Bind("RevenueByClient") %>'/>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
Avatar of dba123
dba123

ASKER

Ok, found a great article here:

http://aspalliance.com/782

so far, I've done this:

......
                <FooterTemplate>
                    <%# GetAZTotal().ToString("N2") %>
                </FooterTemplate>
            </Columns>


and in my code-behind:

    Dim TotalAZBudget As Integer
    Function GetAZBudget(ByVal AZBudget As Integer) As Integer
        TotalAZBudget += AZBudget
        Return AZBudget
    End Function
    Function GetAZBudgetTotal() As Integer
        Return TotalAZBudget
    End Function

Error I'm getting:

Description: An error occurred during the parsing of a resource required to service this request. Please review the following specific parse error details and modify your source file appropriately.

Parser Error Message: System.Web.UI.WebControls.DataControlFieldCollection must have items of type 'System.Web.UI.WebControls.DataControlField'. 'FooterTemplate' is of type 'System.Web.UI.HtmlControls.HtmlGenericControl'.

Source Error:


Line 105:                    </ItemTemplate>
Line 106:                </asp:TemplateField>
Line 107:                <FooterTemplate>
Line 108:                    <%# GetAZTotal().ToString("N2") %>
Line 109:                </FooterTemplate>
 
Inside your Footer template put <asp:Label runat="server" id="lblTotal" />

Then in your codebehind

lblTotal.Text = TotalAZBudget
Avatar of dba123

ASKER

Ok, tried this, still getting error and not sure if I should be placing the AZTotal.ToText - TotalAZBudget in the Page load or not

Parser Error Message: System.Web.UI.WebControls.DataControlFieldCollection must have items of type 'System.Web.UI.WebControls.DataControlField'. 'FooterTemplate' is of type 'System.Web.UI.HtmlControls.HtmlGenericControl'.

Source Error:


Line 105:                    </ItemTemplate>
Line 106:                </asp:TemplateField>
Line 107:                <FooterTemplate>
Line 108:                    <asp:Label runat="server" id="lblAZTotal" />
Line 109:                </FooterTemplate>
 


aspx:
-------

                <FooterTemplate>
                    <asp:Label runat="server" id="lblAZTotal" />
                </FooterTemplate>

code-behind:
------------------

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim lblAZTotal As Label
        lblAZTotal.Text = TotalAZBudget
    End Sub
....

    Dim TotalAZBudget As Integer
    Function GetAZBudget(ByVal AZBudget As Integer) As Integer
        TotalAZBudget += AZBudget
        Return AZBudget
    End Function
    Function GetAZBudgetTotal() As Integer
        Return TotalAZBudget
    End Function
Avatar of dba123

ASKER

and before when I had  <%# GetAZTotal().ToString("N2") %> in there, I got this error:
ASP.NET runtime error: Code blocks are not supported in this context

why was that?
Avatar of dba123

ASKER

I'm all messed up here, hold on...
Avatar of dba123

ASKER

How do I get rid of this error, I still don't understand this damn error message.  Do I need to ad a missing namespace or do I just not have it placed in the correct area within my DataGrid?

Parser Error
Description: An error occurred during the parsing of a resource required to service this request. Please review the following specific parse error details and modify your source file appropriately.

Parser Error Message: System.Web.UI.WebControls.DataControlFieldCollection must have items of type 'System.Web.UI.WebControls.DataControlField'. 'FooterTemplate' is of type 'System.Web.UI.HtmlControls.HtmlGenericControl'.

Source Error:


Line 105:                    </ItemTemplate>
Line 106:                </asp:TemplateField>
Line 107:                <FooterTemplate>
Line 108:                    <asp:Label runat="server" id="AZTotal_Label" />
Line 109:                </FooterTemplate
 
SOLUTION
Avatar of GavinMannion
GavinMannion

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of dba123

ASKER

but won't that create another column?
No it will tell it under which column the footer should reside....

So if you want it to be a total under 'AZ Budget' then have

<asp:TemplateField HeaderText="AZ Budget" SortExpression="Name" ItemStyle-HorizontalAlign="Center">
                  <HeaderStyle HorizontalAlign="Left"></HeaderStyle>
                     <ItemTemplate>
                        <asp:TextBox width="55px"  MaxLength="10" ID="txtAZBudget" Text='<%# Bind("AZBudget") %>' runat="server" />
                    </ItemTemplate>
                       <FooterTemplate>
                          <asp:Label runat="server" id="AZTotal_Label" />
                      </FooterTemplate
                </asp:TemplateField>
Avatar of dba123

ASKER

Ok, the grid is rendering again, but I'm not seeing anything here in the label for AZBudget.

aspx:
----------
        <asp:GridView
         ID="gv_dashboard"
         runat="server"
         AutoGenerateColumns="False"
         DataSourceID="ds_dashboard"
         CellPadding="4"
         ForeColor="#333333"
         ShowFooter="True"  
         GridLines="None"
         CssClass="FormatFont">
     
            <Columns>
                <asp:TemplateField HeaderText="Cust #" SortExpression="CustomerID">
                <HeaderStyle HorizontalAlign="center"></HeaderStyle>
                    <ItemTemplate>
                        <asp:Label runat="server" id="lblCustomerID" Text='<%# Bind("Customer") %>' />
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="AZ Budget" SortExpression="Name" ItemStyle-HorizontalAlign="Center">
                  <HeaderStyle HorizontalAlign="Left"></HeaderStyle>
                     <ItemTemplate>
                        <asp:TextBox width="55px"  MaxLength="10" ID="txtAZBudget" Text='<%# Bind("AZBudget") %>' runat="server" />
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="IL Budget" SortExpression="Name" ItemStyle-HorizontalAlign="Center">
                  <HeaderStyle HorizontalAlign="Left"></HeaderStyle>
                     <ItemTemplate>
                        <asp:TextBox width="55px"  MaxLength="10" ID="txtILBudget" Text='<%# Bind("ILBudget") %>' runat="server" />
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="AZ" SortExpression="Name" ItemStyle-HorizontalAlign="Center">
                 <HeaderStyle HorizontalAlign="center"></HeaderStyle>
                     <ItemTemplate>
                        <asp:checkbox runat="server" Id="chbx_Arizona" Checked='<%# Bind("Arizona") %>'/>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="IL" SortExpression="Name" ItemStyle-HorizontalAlign="Center">
                 <HeaderStyle HorizontalAlign="center"></HeaderStyle>
                     <ItemTemplate>
                        <asp:checkbox runat="server" Id="chbx_Illinois" Checked='<%# Bind("Illinois") %>'/>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Current Month Collections" SortExpression="Name" ItemStyle-HorizontalAlign="Center">
                 <HeaderStyle HorizontalAlign="center"></HeaderStyle>
                     <ItemTemplate>
                        <asp:checkbox runat="server" Id="chbx_CurrentMonthCollections" Checked='<%# Bind("CurrentMonthCollections") %>'/>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Revenue By Month" SortExpression="Name" ItemStyle-HorizontalAlign="Center">
                 <HeaderStyle HorizontalAlign="center"></HeaderStyle>
                     <ItemTemplate>
                        <asp:checkbox runat="server" Id="chbx_RevenueByMonth" Checked='<%# Bind("RevenueByMonth") %>'/>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="PDCs & CCS Monthly" SortExpression="Name" ItemStyle-HorizontalAlign="Center">
                 <HeaderStyle HorizontalAlign="center"></HeaderStyle>
                     <ItemTemplate>
                        <asp:checkbox runat="server" Id="chbx_PDCsCCsMonthly" Checked='<%# Bind("PDCsCCsMonthly") %>'/>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Revenue By Client" SortExpression="Name" ItemStyle-HorizontalAlign="Center">
                 <HeaderStyle HorizontalAlign="center"></HeaderStyle>
                     <ItemTemplate>
                        <asp:checkbox runat="server" Id="chbx_RevenueByClient" Checked='<%# Bind("RevenueByClient") %>'/>
                    </ItemTemplate>
                 <FooterTemplate>
                    <asp:Label runat="server" id="AZTotal_Label" />
                </FooterTemplate>
                </asp:TemplateField>

            </Columns>
           
            <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
            <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
            <EditRowStyle BackColor="#DDDDDD" />
            <SelectedRowStyle BackColor="#DDDDDD" Font-Bold="True" ForeColor="#333333" />
            <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
            <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
            <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
     
        </asp:GridView>


Code-behind:
-----------------

..
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    End Sub
....


    Dim gv_dashboard As GridView

    ' Create a variable to store the order total.
    Private AZBudgetTotal As Integer = 0

    Sub gv_dashboard_RowCreated(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
        If e.Row.RowType = DataControlRowType.Footer Then

            ' Get the AZTotal_Label Label control in the footer row.
            Dim AZTotal As Label = CType(e.Row.FindControl("AZTotal_Label"), Label)

            ' Display the grand total of the AZBudget Column formatted as currency.
            If (Not AZTotal Is Nothing) Then
                AZTotal.Text = AZBudgetTotal.ToString("c0")
            End If

        End If
    End Sub


    Sub gv_dashboard_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)

        If e.Row.RowType = DataControlRowType.DataRow Then

            ' Get the cell that contains the item total.
            Dim cell As TableCell = e.Row.Cells(1)

            ' Get the DataBoundLiteralControl control that contains the
            ' data bound value.
            Dim boundControl As DataBoundLiteralControl = CType(cell.Controls(1), DataBoundLiteralControl)

            ' Remove the '$' character so that the type converter works properly.
            Dim itemTotal As String = boundControl.Text.Replace("$", "")

            ' Add the total for an item (row) to the order total.
            AZBudgetTotal += Convert.ToDecimal(itemTotal)

        End If

    End Sub
Avatar of dba123

ASKER

also, I don't think this is in the right spot still, I feel ilke it should be somewhere else on its own, not in the last ItemTemplateField
               <FooterTemplate>
                    <asp:Label runat="server" id="AZTotal_Label" />
                </FooterTemplate>
To see if it is in the right spot try this

<FooterTemplate>
                    <asp:Label runat="server" id="AZTotal_Label" Text="Testing" />
                </FooterTemplate>

You can then see where it is meant to be...

Try changing

Private AZBudgetTotal As Integer = 0

to

Public AZBudgetTotal As Integer = 0
Avatar of dba123

ASKER

>>>To see if it is in the right spot try this
what do you mean by spot try...


>>>Try changing

Private AZBudgetTotal As Integer = 0

to

Public AZBudgetTotal As Integer = 0

Did that, didn't make a difference when the Grid was rendered...
Avatar of dba123

ASKER

nevermind I see, yea, good idea, let me try that label and see where the footer field is ending up for your spot try
Avatar of dba123

ASKER

ok, cool thanks.  I need to add the footer right here since it will then show below that row then:

                <asp:TemplateField HeaderText="AZ Budget" SortExpression="Name" ItemStyle-HorizontalAlign="Center">
                  <HeaderStyle HorizontalAlign="Left"></HeaderStyle>
                     <ItemTemplate>
                        <asp:TextBox width="55px"  MaxLength="10" ID="txtAZBudget" Text='<%# Bind("AZBudget") %>' runat="server" />
                    </ItemTemplate>
                 <FooterTemplate>
                    <asp:Label runat="server" id="AZTotal_Label" Text="testing" />
                </FooterTemplate>
                </asp:TemplateField>
Okay so it is now in the right place on the grid but still not showing up?

If you put a breakpoint on this line

AZBudgetTotal += Convert.ToDecimal(itemTotal)

Does AZBudgetTotal get incremented?

Also try change
AZTotal.Text = AZBudgetTotal.ToString("c0")
to
AZTotal.Text = AZBudgetTotal.ToString("c:0")
Avatar of dba123

ASKER

If I put a stop point at AZBudgetTotal += Convert.ToDecimal(itemTotal), how can I see the value behind it?  I mouse over it once it gets there but don't see a thing except for it to show "Public AZBudgetTotal As Integer = 0"

Avatar of dba123

ASKER

Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Source Error:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.  


Here's my entire code-behind: http://www.webfound.net/codebehind.txt

Avatar of dba123

ASKER

I think i screwed up the update function when I tried putting this line in there:

    Dim gv_dashboard As GridView

because it didn't understand what gv_dahsboard was when I created the function

 Sub gv_dashboard_RowCreated()

for example.
Yeah you should not need this
Dim gv_dashboard As GridView

Also remember to add

ShowFooter="True"

To your GridView HTML declaration. The easiest way to create the events is to click on your datagrid in Design Time, select properties, click on the lightning bolt and then choose them from there.

Avatar of dba123

ASKER

well, now I'm baffled, wtih the link I gave you to my entire code, without the extra Subs, my UPdate Sub was working fine.  Now it cannot recognize my griview name of gv_dashboard for some odd reason.
>> Now it cannot recognize my griview name of gv_dashboard for some odd reason.
Check the designer file to make sure the control declaration is still there and has the correct name....should be something like:

Protected WithEvents gv_dashboard as System.Web.UI...


Along the same lines as what you're trying to do:
http://msconline.maconstate.edu/tutorials/ASPNET2/ASPNET07/aspnet07-05.aspx


I don't have a snippet to post right now...but you can also accomplish footer totals using *only* the RowDatabound of the gridview at runtime...let me know if you want me to dig it up....

st
Avatar of dba123

ASKER

yea, if you can dig it up thanks, if it takes a long time don't worry about it.  Thanks, yea, I was checking the codegen file and it wasn't in there...
Avatar of dba123

ASKER

what is strange to me is how it disappeared!
Avatar of dba123

ASKER

thanks, I looked back a few days ago at a backed up designer file and it was there.  I'll just put it back in...strange...

    Protected WithEvents gv_dashboard As System.Web.UI.WebControls.GridView
Avatar of dba123

ASKER

Ok, back to why it's not summing.  I'm assuming the array of controls on an aspx page starts with 0
Avatar of dba123

ASKER

Ok, first question.  I still do not see how this is summing anything

    ' Create a variable to store the order total.
    Public AZBudgetTotal As Integer = 0

    Sub gv_dashboard_RowCreated(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
        If e.Row.RowType = DataControlRowType.Footer Then

            ' Get the AZTotal_Label Label control in the footer row.
            Dim AZTotal As Label = CType(e.Row.FindControl("AZTotal_Label"), Label)

            ' Display the grand total of the AZBudget Column formatted as currency.
            If (Not AZTotal Is Nothing) Then
                AZTotal.Text = AZBudgetTotal.ToString("c:0")
            End If

        End If
    End Sub


    Sub gv_dashboard_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)

        If e.Row.RowType = DataControlRowType.DataRow Then

            ' Get the cell that contains the item total.
            Dim cell As TableCell = e.Row.Cells(1)

            ' Get the DataBoundLiteralControl control that contains the
            ' data bound value.
            Dim boundControl As DataBoundLiteralControl = CType(cell.Controls(1), DataBoundLiteralControl)

            ' Remove the '$' character so that the type converter works properly.
            Dim itemTotal As String = boundControl.Text.Replace("$", "")

            ' Add the total for an item (row) to the order total.
            AZBudgetTotal += Convert.ToDecimal(itemTotal)

        End If

    End Sub


Second question

I don't think this is right (Dim cell As TableCell = e.Row.Cells(1)).  This tells me it's trying to grab the total field...my label which I assume would be 2 in the array of items on my page since the footerfield is third in my gridview
Avatar of dba123

ASKER

Looks like the index of the array is based on the DataSource.  In this case then, I don't know what to put for this

Dim cell As TableCell = e.Row.Cells(1)

since the label, which reprents the total value eventually isn't from the datasource...so which index would I have to use for this?

http://scoregasm.blogspot.com/2006/02/aspnet-adding-total-to-gridview.html
A few notes. First, I find it better to index the DataRowView by the column label over the index. The index is based on the DataSource, not the order of the columns in the GridView, so it can be confusing.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of dba123

ASKER

I found a better way to do this using this article: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnaspp/html/GridViewEx09.asp


So I changed my DataGrid to:

        <asp:GridView
         ID="gv_dashboard"
         runat="server"
         AutoGenerateColumns="False"
         DataSourceID="ds_dashboard"
         CellPadding="4"
         ForeColor="#333333"
         ShowFooter="True"  
         GridLines="None"
         CssClass="FormatFont">
     
            <Columns>
                <asp:TemplateField HeaderText="Cust #" SortExpression="CustomerID">
                <HeaderStyle HorizontalAlign="center"></HeaderStyle>
                    <ItemTemplate>
                        <asp:Label runat="server" id="lblCustomerID" Text='<%# Bind("Customer") %>' />
                    </ItemTemplate>
                     <FooterTemplate>
                        <asp:Label runat="server" id="Totals_Label" Text="Totals:" />
                    </FooterTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="AZ Budget" SortExpression="Name" ItemStyle-HorizontalAlign="Center">
                  <HeaderStyle HorizontalAlign="Left"></HeaderStyle>
                     <ItemTemplate>
                        <asp:TextBox width="55px"  MaxLength="10" ID="txtAZBudget" Text='<%# Bind("AZBudget") %>' runat="server" />
                    </ItemTemplate>
                    <FooterTemplate>
                        <asp:Label runat="server" id="AZTotal_Label" />
                    </FooterTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="IL Budget" SortExpression="Name" ItemStyle-HorizontalAlign="Center">
                  <HeaderStyle HorizontalAlign="Left"></HeaderStyle>
                     <ItemTemplate>
                        <asp:TextBox width="55px"  MaxLength="10" ID="txtILBudget" Text='<%# Bind("ILBudget") %>' runat="server" />
                    </ItemTemplate>
                    <FooterTemplate>
                        <asp:Label runat="server" id="ILTotal_Label" />
                    </FooterTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="AZ" SortExpression="Name" ItemStyle-HorizontalAlign="Center">
                 <HeaderStyle HorizontalAlign="center"></HeaderStyle>
                     <ItemTemplate>
                        <asp:checkbox runat="server" Id="chbx_Arizona" Checked='<%# Bind("Arizona") %>'/>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="IL" SortExpression="Name" ItemStyle-HorizontalAlign="Center">
                 <HeaderStyle HorizontalAlign="center"></HeaderStyle>
                     <ItemTemplate>
                        <asp:checkbox runat="server" Id="chbx_Illinois" Checked='<%# Bind("Illinois") %>'/>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Current Month Collections" SortExpression="Name" ItemStyle-HorizontalAlign="Center">
                 <HeaderStyle HorizontalAlign="center"></HeaderStyle>
                     <ItemTemplate>
                        <asp:checkbox runat="server" Id="chbx_CurrentMonthCollections" Checked='<%# Bind("CurrentMonthCollections") %>'/>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Revenue By Month" SortExpression="Name" ItemStyle-HorizontalAlign="Center">
                 <HeaderStyle HorizontalAlign="center"></HeaderStyle>
                     <ItemTemplate>
                        <asp:checkbox runat="server" Id="chbx_RevenueByMonth" Checked='<%# Bind("RevenueByMonth") %>'/>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="PDCs & CCS Monthly" SortExpression="Name" ItemStyle-HorizontalAlign="Center">
                 <HeaderStyle HorizontalAlign="center"></HeaderStyle>
                     <ItemTemplate>
                        <asp:checkbox runat="server" Id="chbx_PDCsCCsMonthly" Checked='<%# Bind("PDCsCCsMonthly") %>'/>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Revenue By Client" SortExpression="Name" ItemStyle-HorizontalAlign="Center">
                 <HeaderStyle HorizontalAlign="center"></HeaderStyle>
                     <ItemTemplate>
                        <asp:checkbox runat="server" Id="chbx_RevenueByClient" Checked='<%# Bind("RevenueByClient") %>'/>
                    </ItemTemplate>
                </asp:TemplateField>

            </Columns>
           
            <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
            <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
            <EditRowStyle BackColor="#DDDDDD" />
            <SelectedRowStyle BackColor="#DDDDDD" Font-Bold="True" ForeColor="#333333" />
            <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
            <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
            <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
     
        </asp:GridView>

and my code-behind to:

    Dim AZBudgetTotal As Integer = 0
    Dim ILBudgetTotal As Integer = 0

    Sub gv_dashboard_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
        If e.Row.RowType = DataControlRowType.DataRow Then
            ' add the UnitPrice and QuantityTotal to the running total variables
            AZBudgetTotal += Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, "AZBudget"))
            ILBudgetTotal += Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "ILBudget"))
        ElseIf e.Row.RowType = DataControlRowType.Footer Then
            e.Row.Cells(1).Text = "Totals:"
            ' for the Footer, display the running totals
            e.Row.Cells(2).Text = AZBudgetTotal.ToString("c:0")
            e.Row.Cells(3).Text = ILBudgetTotal.ToString("c:0")

            e.Row.Cells(2).HorizontalAlign = HorizontalAlign.Right
            e.Row.Cells(3).HorizontalAlign = HorizontalAlign.Right
            e.Row.Font.Bold = True
        End If
    End Sub
lol...your last post was the code I was going to try to dig up... =0D
Avatar of dba123

ASKER

Weird.  I first changed my Total_Label by taking out the Text= portion since the Sub will populate that for me.  But when my form is rendered, no text is rendered for Totals_Label nor any totals for the other 2 footer labels:


        <asp:GridView
         ID="gv_dashboard"
         runat="server"
         AutoGenerateColumns="False"
         DataSourceID="ds_dashboard"
         CellPadding="4"
         ForeColor="#333333"
         ShowFooter="True"  
         GridLines="None"
         CssClass="FormatFont">
     
            <Columns>
                <asp:TemplateField HeaderText="Cust #" SortExpression="CustomerID">
                <HeaderStyle HorizontalAlign="center"></HeaderStyle>
                    <ItemTemplate>
                        <asp:Label runat="server" id="lblCustomerID" Text='<%# Bind("Customer") %>' />
                    </ItemTemplate>
                     <FooterTemplate>
                        <asp:Label runat="server" id="Totals_Label" />
                    </FooterTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="AZ Budget" SortExpression="Name" ItemStyle-HorizontalAlign="Center">
                  <HeaderStyle HorizontalAlign="Left"></HeaderStyle>
                     <ItemTemplate>
                        <asp:TextBox width="55px"  MaxLength="10" ID="txtAZBudget" Text='<%# Bind("AZBudget") %>' runat="server" />
                    </ItemTemplate>
                    <FooterTemplate>
                        <asp:Label runat="server" id="AZTotal_Label" />
                    </FooterTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="IL Budget" SortExpression="Name" ItemStyle-HorizontalAlign="Center">
                  <HeaderStyle HorizontalAlign="Left"></HeaderStyle>
                     <ItemTemplate>
                        <asp:TextBox width="55px"  MaxLength="10" ID="txtILBudget" Text='<%# Bind("ILBudget") %>' runat="server" />
                    </ItemTemplate>
                    <FooterTemplate>
                        <asp:Label runat="server" id="ILTotal_Label" />
                    </FooterTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="AZ" SortExpression="Name" ItemStyle-HorizontalAlign="Center">
                 <HeaderStyle HorizontalAlign="center"></HeaderStyle>
                     <ItemTemplate>
                        <asp:checkbox runat="server" Id="chbx_Arizona" Checked='<%# Bind("Arizona") %>'/>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="IL" SortExpression="Name" ItemStyle-HorizontalAlign="Center">
                 <HeaderStyle HorizontalAlign="center"></HeaderStyle>
                     <ItemTemplate>
                        <asp:checkbox runat="server" Id="chbx_Illinois" Checked='<%# Bind("Illinois") %>'/>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Current Month Collections" SortExpression="Name" ItemStyle-HorizontalAlign="Center">
                 <HeaderStyle HorizontalAlign="center"></HeaderStyle>
                     <ItemTemplate>
                        <asp:checkbox runat="server" Id="chbx_CurrentMonthCollections" Checked='<%# Bind("CurrentMonthCollections") %>'/>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Revenue By Month" SortExpression="Name" ItemStyle-HorizontalAlign="Center">
                 <HeaderStyle HorizontalAlign="center"></HeaderStyle>
                     <ItemTemplate>
                        <asp:checkbox runat="server" Id="chbx_RevenueByMonth" Checked='<%# Bind("RevenueByMonth") %>'/>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="PDCs & CCS Monthly" SortExpression="Name" ItemStyle-HorizontalAlign="Center">
                 <HeaderStyle HorizontalAlign="center"></HeaderStyle>
                     <ItemTemplate>
                        <asp:checkbox runat="server" Id="chbx_PDCsCCsMonthly" Checked='<%# Bind("PDCsCCsMonthly") %>'/>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Revenue By Client" SortExpression="Name" ItemStyle-HorizontalAlign="Center">
                 <HeaderStyle HorizontalAlign="center"></HeaderStyle>
                     <ItemTemplate>
                        <asp:checkbox runat="server" Id="chbx_RevenueByClient" Checked='<%# Bind("RevenueByClient") %>'/>
                    </ItemTemplate>
                </asp:TemplateField>

            </Columns>
           
            <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
            <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
            <EditRowStyle BackColor="#DDDDDD" />
            <SelectedRowStyle BackColor="#DDDDDD" Font-Bold="True" ForeColor="#333333" />
            <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
            <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
            <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
     
        </asp:GridView>

Code-behind:
----------------
    Dim AZBudgetTotal As Integer = 0
    Dim ILBudgetTotal As Integer = 0

    Sub gv_dashboard_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
        If e.Row.RowType = DataControlRowType.DataRow Then
            ' add the UnitPrice and QuantityTotal to the running total variables
            AZBudgetTotal += Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, "AZBudget"))
            ILBudgetTotal += Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "ILBudget"))
        ElseIf e.Row.RowType = DataControlRowType.Footer Then
            e.Row.Cells(0).Text = "Totals:"
            ' for the Footer, display the running totals
            e.Row.Cells(1).Text = AZBudgetTotal.ToString("c:0")
            e.Row.Cells(2).Text = ILBudgetTotal.ToString("c:0")

            e.Row.Cells(1).HorizontalAlign = HorizontalAlign.Right
            e.Row.Cells(2).HorizontalAlign = HorizontalAlign.Right
            e.Row.Font.Bold = True
        End If
    End Sub
Avatar of dba123

ASKER

samtran0331. sweet! yea, I can't believe it's taking me half a day to figure out how to add totals to my gridview.  Oh well, live and learn...that's .NET for you.

Any reason why you think nothing is being rendered here to the lables?  I think I have everything right in my Sub so far as I can see...
Avatar of dba123

ASKER

what the hell is up with Opera anyway, when I paste my URL, it never loads, and stalls...anyway, back to IE I guess.
ps...

Although it's a pretty simple technique...it can be quite powerful to your client...like doing averages...or using the value to change the cell or text color...like if AZBudgetTotal > 50000 for the current row, you can set the column to bold or something...
Avatar of dba123

ASKER

yea, just figuring out why the hell it isn't showing any text in my labels.  How is this Sub invoked?  do I need to invoke it?
you shouldn't need labels in the footer...you're writing the values right into the cell when you do "e.Row.Cells(*).Text"
if you use labels...you'd need to get the label either by FindControl or referencing the control directly before you can set its text
Avatar of dba123

ASKER

so any clues as to why it's not populating my labels...all 3 are not showing a thing in my footerfields:

Here's my latest code:

http://www.webfound.net/index_.txt

http://www.webfound.net/codebehind_gridview.txt
Avatar of dba123

ASKER

I don't see why it's not populating the labels
Avatar of dba123

ASKER

oh, I see...let me take the lables out...
...I guess I didn't really answer your last 2 questions:
> How is this Sub invoked?  do I need to invoke it?
Any time you bind your grid, the  gv_dashboard_RowDataBound will be invoked.
so, no, you don't need to do anything else beside bind the grid to fire off the  gv_dashboard_RowDataBound

Avatar of dba123

ASKER

Ok but if I take the lables out, basically I'll have 3 of these...then I assume it's referencing them by index then at that point which is 0, 1, and 2 starting from top down?

                <HeaderStyle HorizontalAlign="center"></HeaderStyle>
                    <ItemTemplate>
                        <asp:Label runat="server" id="lblCustomerID" Text='<%# Bind("Customer") %>' />
                    </ItemTemplate>
                     <FooterTemplate>

                    </FooterTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="AZ Budget" SortExpression="Name" ItemStyle-HorizontalAlign="Center">
                  <HeaderStyle HorizontalAlign="Left"></HeaderStyle>
                     <ItemTemplate>
                        <asp:TextBox width="55px"  MaxLength="10" ID="txtAZBudget" Text='<%# Bind("AZBudget") %>' runat="server" />
                    </ItemTemplate>
                    <FooterTemplate>

                    </FooterTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="IL Budget" SortExpression="Name" ItemStyle-HorizontalAlign="Center">
                  <HeaderStyle HorizontalAlign="Left"></HeaderStyle>
                     <ItemTemplate>
                        <asp:TextBox width="55px"  MaxLength="10" ID="txtILBudget" Text='<%# Bind("ILBudget") %>' runat="server" />
                    </ItemTemplate>
                    <FooterTemplate>

                    </FooterTemplate>
                </asp:TemplateField>
Avatar of dba123

ASKER

this is driving me batty.  I took out the labels in my GridView as you see above.  All other code is the same as I posted via those 2 links.  Why isn't it rendering anytying in those FooterTemplates?  

ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of dba123

ASKER

so you didn't even use footertemplate...why?
Avatar of dba123

ASKER

AHHHHHHHHHHHH, I was missing Handles gv_dashboard.RowDataBound

What exactly is that doing?
Avatar of dba123

ASKER

it's working! thanks a lot!
>>What exactly is that doing?
If you're using codebehind...the "handles" part is what wires up the subroutine to the datagrid

you could have just as well named the sub something else and left off the Handle like:

Private Sub keepRunningSum(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs)
        Try
            If e.Row.RowType = DataControlRowType.DataRow Then
                dblAD1 += Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, "sumProd"))
....etc....
    End Sub

And then in your datagrid/gridview on the aspx page set the tag:
<asp:grdiview OnRowDatabound="keepRunningSum".....> etc....