Go Back   DeveloperBarn Forums > Programming & Scripting > .Net Development

Sponsored Links

Discuss "add additional footer to gridview" in the .Net Development forum.

.Net Development - Learn about the Microsoft.Net framework and how to create powerful web-based (ASP.net) and client-based (Windows Forms) applications utilizing various languages such as C#, VB.Net, J# and others.


Closed Thread « Previous Thread | Next Thread »
 
LinkBack Thread Tools Display Modes
  #1  
Old May 2nd, 2008, 08:57 AM
Barn Frequenter

 
Join Date: Mar 2008
Posts: 174
Thanks: 14
Thanked 0 Times in 0 Posts
Rep Power: 1
peebman2000 is an unknown quantity at this point
Question 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
Sponsored Links
  #2  
Old May 2nd, 2008, 09:13 AM
jmurrayhead's Avatar
The Barnfather

 
Join Date: Mar 2008
Location: Reston, VA, USA
Posts: 820
Thanks: 20
Thanked 74 Times in 71 Posts
Blog Entries: 5
Rep Power: 3
jmurrayhead has a spectacular aura aboutjmurrayhead has a spectacular aura aboutjmurrayhead has a spectacular aura about

Awards Showcase
Microsoft .Net Microsoft SQL Server Microsoft Windows Classic ASP 
Total Awards: 4

Default

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 with me... click the icon!
If my post solved your problem, click the button in the lower right-hand corner of the post.

Join our Folding team: DeveloperBarn Folding
  #3  
Old May 2nd, 2008, 10:14 AM
Barn Frequenter

 
Join Date: Mar 2008
Posts: 174
Thanks: 14
Thanked 0 Times in 0 Posts
Rep Power: 1
peebman2000 is an unknown quantity at this point
Question 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  
Old May 2nd, 2008, 10:23 AM
jmurrayhead's Avatar
The Barnfather

 
Join Date: Mar 2008
Location: Reston, VA, USA
Posts: 820
Thanks: 20
Thanked 74 Times in 71 Posts
Blog Entries: 5
Rep Power: 3
jmurrayhead has a spectacular aura aboutjmurrayhead has a spectacular aura aboutjmurrayhead has a spectacular aura about

Awards Showcase
Microsoft .Net Microsoft SQL Server Microsoft Windows Classic ASP 
Total Awards: 4

Default

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.
  #5  
Old May 2nd, 2008, 10:32 AM
Barn Frequenter

 
Join Date: Mar 2008
Posts: 174
Thanks: 14
Thanked 0 Times in 0 Posts
Rep Power: 1
peebman2000 is an unknown quantity at this point
Default 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  
Old May 2nd, 2008, 10:42 AM
jmurrayhead's Avatar
The Barnfather

 
Join Date: Mar 2008
Location: Reston, VA, USA
Posts: 820
Thanks: 20
Thanked 74 Times in 71 Posts
Blog Entries: 5
Rep Power: 3
jmurrayhead has a spectacular aura aboutjmurrayhead has a spectacular aura aboutjmurrayhead has a spectacular aura about

Awards Showcase
Microsoft .Net Microsoft SQL Server Microsoft Windows Classic ASP 
Total Awards: 4

Default

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)
  #7  
Old May 2nd, 2008, 11:13 AM
Barn Frequenter

 
Join Date: Mar 2008
Posts: 174
Thanks: 14
Thanked 0 Times in 0 Posts
Rep Power: 1
peebman2000 is an unknown quantity at this point
Default 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  
Old May 2nd, 2008, 12:44 PM
jmurrayhead's Avatar
The Barnfather

 
Join Date: Mar 2008
Location: Reston, VA, USA
Posts: 820
Thanks: 20
Thanked 74 Times in 71 Posts
Blog Entries: 5
Rep Power: 3
jmurrayhead has a spectacular aura aboutjmurrayhead has a spectacular aura aboutjmurrayhead has a spectacular aura about

Awards Showcase
Microsoft .Net Microsoft SQL Server Microsoft Windows Classic ASP 
Total Awards: 4

Default

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.
  #9  
Old May 2nd, 2008, 01:07 PM
Barn Frequenter

 
Join Date: Mar 2008
Posts: 174
Thanks: 14
Thanked 0 Times in 0 Posts
Rep Power: 1
peebman2000 is an unknown quantity at this point
Question 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:

Quote:
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  
Old May 2nd, 2008, 01:12 PM
jmurrayhead's Avatar
The Barnfather

 
Join Date: Mar 2008
Location: Reston, VA, USA
Posts: 820
Thanks: 20
Thanked 74 Times in 71 Posts
Blog Entries: 5
Rep Power: 3
jmurrayhead has a spectacular aura aboutjmurrayhead has a spectacular aura aboutjmurrayhead has a spectacular aura about

Awards Showcase
Microsoft .Net Microsoft SQL Server Microsoft Windows Classic ASP 
Total Awards: 4

Default

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
Closed Thread

  DeveloperBarn Forums > Programming & Scripting > .Net Development

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT -4. The time now is 08:27 PM.



Content Relevant URLs by vBSEO 3.2.0