Average information in Gridviews Footer

J

joel.sjoo

Hello. I'm quite new to asp.net and programming. Anyway, I'm trying to
display average information in a Gridviews footer on one of the
columns. It works well with summarizing.

I use a simple Gridview and I have tried some code from the tutorials
at asp.net website below. I can't figure out how I should change the
code to work for my sqldatasource. In their code I think the work
against an objectdatasource or something. If anyone could help me I
would be grateful.

Regard Joel


Dim _totalPrice As Decimal = 0
Dim _totalNonNullUnitPriceCount As Integer = 0
Dim _totalUnitsInStock As Integer = 0
Dim _totalUnitsOnOrder As Integer = 0
Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e
As GridViewRowEventArgs) Handles GridView1.RowDataBound

If e.Row.RowType = DataControlRowType.DataRow Then
Dim product As Northwind.ProductsRow = _
CType(CType(e.Row.DataItem, DataRowView).Row,
Northwind.ProductsRow)

If Not product.IsUnitPriceNull() Then
_totalPrice += product.UnitPrice
_totalNonNullUnitPriceCount += 1
End If

If Not product.IsUnitsInStockNull() Then
_totalUnitsInStock += product.UnitsInStock
End If

If Not product.IsUnitsOnOrderNull() Then
_totalUnitsOnOrder += product.UnitsOnOrder
End If
ElseIf e.Row.RowType = DataControlRowType.Footer Then
Dim avgPrice As Decimal = _
_totalUnitPrice / CType(_totalNonNullUnitPriceCount,
Decimal)

e.Row.Cells(1).Text = "Avg.: " & avgPrice.ToString("c")

End If
End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,969
Messages
2,570,161
Members
46,710
Latest member
bernietqt

Latest Threads

Top