Average information in Gridviews Footer

Discussion in 'ASP .Net' started by joel.sjoo@gmail.com, Jul 31, 2007.

  1. Guest

    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
    , Jul 31, 2007
    #1
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    0
    Views:
    4,776
  2. Roy
    Replies:
    0
    Views:
    1,179
  3. dorayme
    Replies:
    3
    Views:
    407
    dorayme
    Apr 3, 2009
  4. Henrik
    Replies:
    1
    Views:
    424
    Ken Cox [Microsoft MVP]
    Jul 6, 2006
  5. Whitney
    Replies:
    1
    Views:
    129
    Thomas 'PointedEars' Lahn
    Aug 3, 2004
Loading...

Share This Page