Closed Thread
Page 1 of 2 1 2 LastLast
Results 1 to 10 of 14

Thread: add additional footer to gridview

  1. #1
    Barn Enthusiast peebman2000 is on a distinguished road peebman2000's Avatar
    Join Date
    Mar 2008
    Posts
    210
    Rep Power
    3

    add additional footer to gridview

    Hey everyone is peebman2000, currently working on a .net app questionaire. The client wants reports, that give them the tally or count of the number of answers people selected and the percentage that people selecting that answer.

    I have code that will calulate the percentage as totals in the footer, but I need another footer added in the gridview to show the totals.

    I was told before by someone on the forum, I could use Ajax to do something with the gridview to add the additional footer. I need to find out how to add the additional footer with totals along with the percentages.

    I've looked online, I found some ideas and code to use the OnRowCreated method, but that didn't work for me.

    Does anyone know how I can add an additional footer in a gridview?

    I use the gridview to populate the clients information and then allow them to create the report by exporting it into an excel file.

    Any help would be greatly appreciated, thanks so much.

    My current small code:
    Code:
    Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
            'Dim priceTotal As Decimal = 0'''CALCULATE TOTALS IN FOOTER
            Dim priceTotal2 As Decimal = 0
            
    
            If e.Row.RowType = DataControlRowType.DataRow Then
                ' add the UnitPrice and QuantityTotal to the running total variables
                Total += GetUnitPrice9(Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, "Total")))
                
                priceTotal2 += GetUnitPrice(Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, "Answer_1")))
               
            ElseIf e.Row.RowType = DataControlRowType.Footer Then
                e.Row.Cells(0).Text = "Totals:"
                ' for the Footer, display the running totals
                e.Row.Cells(2).Text = Total.ToString()
                e.Row.Cells(3).Text = Round(((GetTotal() / Total) * 100), 1).ToString + "%"
    
              
    
    
    
                e.Row.Cells(1).HorizontalAlign = HorizontalAlign.Center
                
                e.Row.Font.Bold = True
    
            End If
    
    
        End Sub
    
    Dim TotalUnitPrice As Decimal = 0.0 
    
    Function GetUnitPrice(ByVal Price As Decimal) As Decimal
            TotalUnitPrice += Price
            Return Price
        End Function
        Function GetTotal() As Decimal
            Return TotalUnitPrice
        End Function
    
    
    Function GetUnitPrice9(ByVal Price As Decimal) As Decimal
            TotalUnitPrice9 += Price
            Return Price
        End Function
        Function GetTotal9() As Decimal
            Return TotalUnitPrice9
        End Function
    

  2. #2
    The Barnfather jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead's Avatar
    Join Date
    Mar 2008
    Location
    Washington, D.C.
    Posts
    2,347
    Blog Entries
    9
    Rep Power
    19

    As far as I know, the GridView is limited to 1 footer only. This is why the Repeater control is your friend. You can make it do anything a GridView can and then some.

    Maybe clarify for us on how this is supposed to look.
    jmurrayhead
    If you agree, give me rep. If my post helped you, click "Thanks".
    If you like it here...throw us a few bones to help support us.


  3. #3
    Barn Enthusiast peebman2000 is on a distinguished road peebman2000's Avatar
    Join Date
    Mar 2008
    Posts
    210
    Rep Power
    3

    reply

    thanks jmurrayhead, i've never used the repeater. Can I bind data to the repeater like I would a gridview? Also I use the gridview to export data from it to a excel spreadsheet. Would I be able to export data from a repeater?


    How i want it to look would be like this:


    Thanks let me know.

    Quote Originally Posted by jmurrayhead View Post
    As far as I know, the GridView is limited to 1 footer only. This is why the Repeater control is your friend. You can make it do anything a GridView can and then some.

    Maybe clarify for us on how this is supposed to look.

  4. #4
    The Barnfather jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead's Avatar
    Join Date
    Mar 2008
    Location
    Washington, D.C.
    Posts
    2,347
    Blog Entries
    9
    Rep Power
    19

    Okay, for GridView, it looks like you want to do something like this: CodeSnip: How to Display Sum Total in the Footer of the GridView Control: ASP Alliance

    As far as the Repeater control, yes you can databind to it and I don't see any reason why you couldn't somehow output the data to an Excel document.
    jmurrayhead
    If you agree, give me rep. If my post helped you, click "Thanks".
    If you like it here...throw us a few bones to help support us.


  5. #5
    Barn Enthusiast peebman2000 is on a distinguished road peebman2000's Avatar
    Join Date
    Mar 2008
    Posts
    210
    Rep Power
    3

    reply

    Hey thanks, but I have that footer snip of code, i'm actually using it to get my totals in the footer of my gridview, but I need another footer to show my percentages.

    So in a repeater, i'm playing with the repeater now; I've never used it before. So what goes in between the repeater control? And i'll look up some links online on the control, but it seem not like your normal asp.net control.


    Quote Originally Posted by jmurrayhead View Post
    Okay, for GridView, it looks like you want to do something like this: CodeSnip: How to Display Sum Total in the Footer of the GridView Control: ASP Alliance

    As far as the Repeater control, yes you can databind to it and I don't see any reason why you couldn't somehow output the data to an Excel document.

  6. #6
    The Barnfather jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead's Avatar
    Join Date
    Mar 2008
    Location
    Washington, D.C.
    Posts
    2,347
    Blog Entries
    9
    Rep Power
    19

    Here's an example of how I repeater would look:

    Code:
    <asp:Repeater ID="Repeater1" runat="server">
        <HeaderTemplate>
            <table>
                <tr>
                    <th>Header1</th><th>Header2</th>
                </tr>
        </HeaderTemplate>
        <ItemTemplate>
            <tr>
                <td><asp:Label ID="Label1" runat="server" /></td>
                <td><asp:Label ID="Label2" runat="server" /></td>
            </tr>
        </ItemTemplate>
        <AlternatingItemTemplate>
            <tr class="altrow">
                <td><asp:Label ID="Label1" runat="server" /></td>
                <td><asp:Label ID="Label2" runat="server" /></td>
            </tr>
        </AlternatingItemTemplate>
        <FooterTemplate>
                <tr>
                    <td>Some Data</td><td>Some Data</td>
                 </tr>
                 <tr>
                    <td>Some Data</td><td>Some Data</td>
                 </tr>
             </table>
        </FooterTemplate>
    </asp:Repeater>
    
    There are several ways you can bind to this control, including using the datasource controls or creating a DataTable and bind the repeater to the DataTable. It's highly customizeable AND it has it's own events.

    Find out more about the Repeater control here: Repeater Class (System.Web.UI.WebControls)
    jmurrayhead
    If you agree, give me rep. If my post helped you, click "Thanks".
    If you like it here...throw us a few bones to help support us.


  7. #7
    Barn Enthusiast peebman2000 is on a distinguished road peebman2000's Avatar
    Join Date
    Mar 2008
    Posts
    210
    Rep Power
    3

    reply

    Okay I see how that works, that should work, but how would I do the totals. Because in the gridview the datasource query does the counts or tallying of the answers. The GetUnitPrice function gives you the totals of the tallied data in the footer. So how would I do this........

    You know what I kind of had a brain freeze, I guess I could use a datatable and calculate the totals and percentage from there. How does that sound?


    Quote Originally Posted by jmurrayhead View Post
    Here's an example of how I repeater would look:

    Code:
    <asp:Repeater ID="Repeater1" runat="server">
        <HeaderTemplate>
            <table>
                <tr>
                    <th>Header1</th><th>Header2</th>
                </tr>
        </HeaderTemplate>
        <ItemTemplate>
            <tr>
                <td><asp:Label ID="Label1" runat="server" /></td>
                <td><asp:Label ID="Label2" runat="server" /></td>
            </tr>
        </ItemTemplate>
        <AlternatingItemTemplate>
            <tr class="altrow">
                <td><asp:Label ID="Label1" runat="server" /></td>
                <td><asp:Label ID="Label2" runat="server" /></td>
            </tr>
        </AlternatingItemTemplate>
        <FooterTemplate>
                <tr>
                    <td>Some Data</td><td>Some Data</td>
                 </tr>
                 <tr>
                    <td>Some Data</td><td>Some Data</td>
                 </tr>
             </table>
        </FooterTemplate>
    </asp:Repeater>
    
    There are several ways you can bind to this control, including using the datasource controls or creating a DataTable and bind the repeater to the DataTable. It's highly customizeable AND it has it's own events.

    Find out more about the Repeater control here: Repeater Class (System.Web.UI.WebControls)

  8. #8
    The Barnfather jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead's Avatar
    Join Date
    Mar 2008
    Location
    Washington, D.C.
    Posts
    2,347
    Blog Entries
    9
    Rep Power
    19

    Quote Originally Posted by peebman2000 View Post
    You know what I kind of had a brain freeze, I guess I could use a datatable and calculate the totals and percentage from there. How does that sound?
    That is one possibility. Another possibility would be to place some labels or whatever you want to use to display the data. Then, in the ItemDataBound event, use these controls as a holding place for the count and just add to the existing value.
    jmurrayhead
    If you agree, give me rep. If my post helped you, click "Thanks".
    If you like it here...throw us a few bones to help support us.


  9. #9
    Barn Enthusiast peebman2000 is on a distinguished road peebman2000's Avatar
    Join Date
    Mar 2008
    Posts
    210
    Rep Power
    3

    reply

    jmurrayhead, thanks for the help, but this what I got. I've created a sql dat source that shows my practice repeater information, heres an image of shown when I compile or run the aspx page:


    Now I want to calculate the totals for the Received columns and the Answars column. I tried using the same code I used to calculate the totals in the gridview, but I get an error:

    Server Error in '/questionaire' Application.
    --------------------------------------------------------------------------------

    Operator '=' is not defined for type 'DataRowView' and type 'DataControlRowType'.
    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.InvalidCastException: Operator '=' is not defined for type 'DataRowView' and type 'DataControlRowType'.

    Source Error:


    Line 22: Dim total2 As Decimal = 0
    Line 23:
    Line 24: If e.Item.DataItem = DataControlRowType.DataRow Then
    Line 25: Total += GetUnitPrice(Convert.ToDecimal(DataBinder.Eval(e.I tem.DataItem, "received")))
    Line 26: total2 += GetUnitPrice(Convert.ToDecimal(DataBinder.Eval(e.I tem.DataItem, "ansers")))


    Source File: C:\Documents and Settings\DPeebles\Desktop\wizard\questionaire\addr owgridview.aspx.vb Line: 24

    Here the repeater source code: Aspx page
    Code:
    <form id="form1" runat="server">
        <div>
            </div>
            <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:peebman %>" SelectCommand="SELECT COUNT(id) AS received, COUNT(cost) AS ansers, product
     FROM dbo.product 
     group by product"></asp:SqlDataSource>
            
            <asp:Repeater ID="Repeater1" runat="server" DataSourceID="SqlDataSource1">
            <HeaderTemplate>
                    <table border=1>
                      <tr>
                        <td><b>Received</b></td>
                        <td><b>Answars</b></td>
                        <td><b>Product</b></td>
                      </tr>
    
            </HeaderTemplate>
            <ItemTemplate>
            <tr>
                      <td> <%#DataBinder.Eval(Container.DataItem, "received")%> </td>
                      <td> <%#DataBinder.Eval(Container.DataItem, "ansers")%> </td>
                      <td> <%# DataBinder.Eval(Container.DataItem, "product") %> </td>
                    </tr>
    
                
                
            </ItemTemplate>
            </asp:Repeater>
        </form>
    
    this is my code for aspx.vb page:
    Code:
    Dim Total As Integer = 0
        Protected Sub Repeater1_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles Repeater1.ItemDataBound
            Dim total2 As Decimal = 0
    
            If e.Item.DataItem = DataControlRowType.DataRow Then
                Total += GetUnitPrice(Convert.ToDecimal(DataBinder.Eval(e.Item.DataItem, "received")))
                total2 += GetUnitPrice(Convert.ToDecimal(DataBinder.Eval(e.Item.DataItem, "ansers")))
            ElseIf e.Item.DataItem = DataControlRowType.Footer Then
    
                e.Item.DataItem.cell(0).text = Total.ToString()
                e.Item.DataItem.cell(1).text = total2.ToString()
    
    
    
    
    
            End If
        End Sub
        Dim TotalUnitPrice As Decimal = 0.0 '''''CALULATE TOTATS FOR EACH CELL IN GRIDVIEW
        Dim TotalUnitPrice2 As Decimal = 0.0
    
        Function GetUnitPrice(ByVal Price As Decimal) As Decimal
            TotalUnitPrice += Price
            Return Price
        End Function
        Function GetTotal() As Decimal
            Return TotalUnitPrice
        End Function
        Function GetUnitPrice2(ByVal Price As Decimal) As Decimal
            TotalUnitPrice2 += Price
            Return Price
        End Function
        Function GetTotal2() As Decimal
            Return TotalUnitPrice2
        End Function
    
    End Class
    
    Any thoughts on the error, i know i'm doing this wrong, but I'm not for sure how to get around it. I'm still looking on line, but some of the things I found is a little unclear to me.

    Thanks.
    Quote Originally Posted by jmurrayhead View Post
    That is one possibility. Another possibility would be to place some labels or whatever you want to use to display the data. Then, in the ItemDataBound event, use these controls as a holding place for the count and just add to the existing value.

  10. #10
    The Barnfather jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead's Avatar
    Join Date
    Mar 2008
    Location
    Washington, D.C.
    Posts
    2,347
    Blog Entries
    9
    Rep Power
    19

    You will want to check the row type like this in a repeater:

    Code:
    If e.Item.RowType = ListItemType.Item Or e.Item.RowType = ListItemType.AlternatingItem Then
        ....
    ElseIf e.Item.RowType = ListItemType.Footer Then
        ....
    End If
    
    jmurrayhead
    If you agree, give me rep. If my post helped you, click "Thanks".
    If you like it here...throw us a few bones to help support us.


Closed Thread
Page 1 of 2 1 2 LastLast

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

SEO by vBSEO