putting average of column in footer, I can get the sum
I am able to sum an entire column and put the total in a column, but what if i want to get the average of all the numbers in the column?
Dim paxaverage As Decimal = 0.0 Function GetAverage(ByVal hope As Decimal) As Decimal 'this is where i am stuck paxaverage += hope / saods.Tables("salesasoftable")Rows.Count Return hope End Function Function Getaverage1() As Decimal Return paxaverage End Function
<asp:TemplateField HeaderText="Remaining PAX"> <ItemTemplate> <%#GetAverage(Decimal.Parse(Eval("RemainingPax").ToString())).ToString("N0")%> </ItemTemplate> <FooterTemplate> <%#GetAverage1().ToString("N0")%> </FooterTemplate> <FooterStyle Font-Bold="True" /> </asp:TemplateField>
This article discusses a couple of approaches to this problem and gives you some sample code. The one I've used in the past is to have a couple of private fields in the Page code-behind: one for the count and one for the sum. You can then increment these in the GridView's RowDataBound event when the row in question is a data row. When it is a footer row, you compute the average and display it in the right cell.