Calculated cell in GridView

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • gchq
    New Member
    • Jan 2007
    • 96

    #1

    Calculated cell in GridView

    Using both static data and data from some GridView cells I am attempting to calculate a result in a Template Cell - but to no avail!

    The value for TextBox1 is brought in on the page_load event and I'm attempting the balance of the calculation uisng the ItemDataBound event :-

    Code:
    Protected Sub GridView1_ItemDataBound(ByVal sender As Object, ByVal e As DataGridItemEventArgs)
            If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem Then
                Dim vInterest As Single = TextBox1.Text / 100.0#
                Dim vRate As Single = 1.0#
                Dim vRemLife As Single = e.Item.Cells(3).Text
                Dim vCostAv As Single = e.Item.Cells(4).Text
                Dim vNum As Integer
                For vNum = 1 To vRemLife
                    vRate = vRate * (1 + vInterest)
                Next vNum
                Dim vInsert As Single = Format(vCostAv * vRate, "###.##.0.00")
                e.Item.Cells(5).Text = vInsert
            End If
    But the calculated cells are always empty at run time! Any ideas what where I'm going wrong?
  • Killer42
    Recognized Expert Expert
    • Oct 2006
    • 8429

    #2
    Have you stepped through the code to make sure it's doing what you expect, and when you expect it?

    Comment

    • gchq
      New Member
      • Jan 2007
      • 96

      #3
      Originally posted by Killer42
      Have you stepped through the code to make sure it's doing what you expect, and when you expect it?
      I re-jigged it - and now it works :-)

      Code:
      Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
              If e.Row.RowType = DataControlRowType.DataRow Then
                  Dim vInterest As Single = TextBox1.Text / 100.0#
                  Dim vRate As Single = 1.0#
                  Dim vRemLife As Single = Convert.ToSingle(DataBinder.Eval(e.Row.DataItem, "Rem_Life"))
                  Dim vCostAv As Single = Convert.ToSingle(DataBinder.Eval(e.Row.DataItem, "Cost_Av"))
                  Dim vNum As Integer
                  For vNum = 1 To vRemLife
                      vRate = vRate * (1 + vInterest)
                  Next vNum
                  Dim vInsert As Single = vCostAv * vRate
                  Dim vInsert2 As String = Format(vInsert, "###,##0.00")
                  e.Row.Cells(6).Text = vInsert2
              End If
          End Sub

      Comment

      • Killer42
        Recognized Expert Expert
        • Oct 2006
        • 8429

        #4
        Ok. Glad to see you've resolved it.

        Comment

        Working...