[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 4438
  • Last Modified:

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>
0
dba123
Asked:
dba123
  • 34
  • 8
  • 7
3 Solutions
 
dba123Author Commented:
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>
 
0
 
GavinMannionCommented:
Inside your Footer template put <asp:Label runat="server" id="lblTotal" />

Then in your codebehind

lblTotal.Text = TotalAZBudget
0
 
dba123Author Commented:
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
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
dba123Author Commented:
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?
0
 
dba123Author Commented:
I'm all messed up here, hold on...
0
 
GavinMannionCommented:
0
 
dba123Author Commented:
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
 
0
 
GavinMannionCommented:
Put your <FooterTemplate inside your <asp:templatefield not under it.
0
 
dba123Author Commented:
but won't that create another column?
0
 
GavinMannionCommented:
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>
0
 
dba123Author Commented:
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
0
 
dba123Author Commented:
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>
0
 
GavinMannionCommented:
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
0
 
dba123Author Commented:
>>>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...
0
 
dba123Author Commented:
nevermind I see, yea, good idea, let me try that label and see where the footer field is ending up for your spot try
0
 
dba123Author Commented:
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>
0
 
GavinMannionCommented:
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")
0
 
dba123Author Commented:
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"

0
 
dba123Author Commented:
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

0
 
dba123Author Commented:
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.
0
 
GavinMannionCommented:
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.

0
 
dba123Author Commented:
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.
0
 
samtran0331Commented:
>> 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
0
 
dba123Author Commented:
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...
0
 
dba123Author Commented:
what is strange to me is how it disappeared!
0
 
dba123Author Commented:
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
0
 
dba123Author Commented:
Ok, back to why it's not summing.  I'm assuming the array of controls on an aspx page starts with 0
0
 
dba123Author Commented:
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
0
 
dba123Author Commented:
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.
0
 
GavinMannionCommented:
The first Sub that you have is actually just assigning the value to your footer label.

The second part is taking the second control in the second cell and getting it's value.

From your original datagrid I don't think that is correct.
Try

Dim t as TextBox = CType(e.Row.Cells(1).Controls(0), TextBox)
0
 
dba123Author Commented:
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
0
 
samtran0331Commented:
lol...your last post was the code I was going to try to dig up... =0D
0
 
dba123Author Commented:
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
0
 
dba123Author Commented:
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...
0
 
dba123Author Commented:
what the hell is up with Opera anyway, when I paste my URL, it never loads, and stalls...anyway, back to IE I guess.
0
 
samtran0331Commented:
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...
0
 
dba123Author Commented:
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?
0
 
samtran0331Commented:
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
0
 
dba123Author Commented:
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
0
 
dba123Author Commented:
I don't see why it's not populating the labels
0
 
dba123Author Commented:
oh, I see...let me take the lables out...
0
 
samtran0331Commented:
...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

0
 
dba123Author Commented:
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>
0
 
dba123Author Commented:
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?  

0
 
samtran0331Commented:
pulled this from a working page...


gv:
            <asp:GridView ID="gvAgentDetails" runat="server" AutoGenerateColumns="False" Width="100%">
                <Columns>
                    <asp:TemplateField HeaderStyle-HorizontalAlign="Left" ItemStyle-HorizontalAlign="Left">
                        <HeaderTemplate>
                            Date
                        </HeaderTemplate>
                        <ItemTemplate>
                            <asp:Label ID="lblStatementDate" runat="server" Text='<%# CType(Eval("StatementDate"),String).Replace("12:00:00 AM", "") %>'></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderStyle-HorizontalAlign="Left" ItemStyle-HorizontalAlign="Left">
                        <HeaderTemplate>
                            Agent
                        </HeaderTemplate>
                        <ItemTemplate>
                            <asp:Label ID="lblAgent" runat="server" Text='<%# Eval("PFullName") %>'></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderStyle-HorizontalAlign="Left" ItemStyle-HorizontalAlign="Left">
                        <HeaderTemplate>
                            Policy Num.
                        </HeaderTemplate>
                        <ItemTemplate>
                            <asp:Label ID="lblPolicyNum" runat="server" Text='<%# Eval("PolicyNum") %>'></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderStyle-HorizontalAlign="Left" ItemStyle-HorizontalAlign="Left">
                        <HeaderTemplate>
                            Company
                        </HeaderTemplate>
                        <ItemTemplate>
                            <asp:Label ID="lblProducerName" runat="server" Text='<%# Eval("ProducerName") %>'></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderStyle-HorizontalAlign="Left" ItemStyle-HorizontalAlign="Left">
                        <HeaderTemplate>
                            Product
                        </HeaderTemplate>
                        <ItemTemplate>
                            <asp:Label ID="lblProductName" runat="server" Text='<%# Eval("ProductName") %>'></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderStyle-HorizontalAlign="Right" ItemStyle-HorizontalAlign="Right">
                        <HeaderTemplate>
                            Production
                        </HeaderTemplate>
                        <ItemTemplate>
                            <asp:Label ID="lblProduction" runat="server" Text='<%# Eval("sumProd", "{0:C}") %>'></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderStyle-HorizontalAlign="Right" ItemStyle-HorizontalAlign="Right">
                        <HeaderTemplate>
                            Commissions
                        </HeaderTemplate>
                        <ItemTemplate>
                            <asp:Label ID="lblCommissions" runat="server" Text='<%# Eval("sumComm", "{0:C}") %>'></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>
                </Columns>
            </asp:GridView>


code:

    Dim dblAD1 As Decimal = 0.0
    Dim dblAD2 As Decimal = 0.0
    Private Sub gvAgentDetails_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gvAgentDetails.RowDataBound
        Try
            If e.Row.RowType = DataControlRowType.DataRow Then
                dblAD1 += Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, "sumProd"))
                dblAD2 += Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, "sumComm"))
            ElseIf e.Row.RowType = DataControlRowType.Footer Then
                e.Row.Font.Bold = True
                e.Row.Cells(4).Text = "Totals:"
                e.Row.Cells(5).Text = String.Format("{0:c}", dblAD1)
                e.Row.Cells(6).Text = String.Format("{0:c}", dblAD2)
                e.Row.Cells(4).HorizontalAlign = HorizontalAlign.Right
                e.Row.Cells(5).HorizontalAlign = HorizontalAlign.Right
                e.Row.Cells(6).HorizontalAlign = HorizontalAlign.Right
            End If
        Catch ex As Exception
            Response.Write("gvAD_RDB: " & ex.Message.ToString)
            Exit Sub
        End Try
    End Sub
0
 
dba123Author Commented:
so you didn't even use footertemplate...why?
0
 
dba123Author Commented:
AHHHHHHHHHHHH, I was missing Handles gv_dashboard.RowDataBound

What exactly is that doing?
0
 
dba123Author Commented:
it's working! thanks a lot!
0
 
samtran0331Commented:
>>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....
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 34
  • 8
  • 7
Tackle projects and never again get stuck behind a technical roadblock.
Join Now