This is a discussion on add additional footer to gridview within the .Net Development forums, part of the Programming & Scripting category; Hey everyone is peebman2000, currently working on a .net app questionaire. The client wants reports, that give them the tally ...
| |||||||
|
#1
| |||
| |||
| 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
| ||||
| ||||
| 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.If you like it here...throw us a few bones to help support us. Join our Folding team: DeveloperBarn Folding |
|
#3
| |||
| |||
| 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. |
|
#4
| ||||
| ||||
| 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
| |||
| |||
| 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:
|
|
#6
| ||||
| ||||
| 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>
Find out more about the Repeater control here: Repeater Class (System.Web.UI.WebControls) |
|
#7
| |||
| |||
| 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:
|
|
#8
| ||||
| ||||
| 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
| |||
| |||
| 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:
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>
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
Thanks. |
|
#10
| ||||
| ||||
| 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
|
![]() |
|
| Bookmarks |
| Thread Tools | |
| Display Modes | |
| |