Conversion of a BoundField.DataField

Last post 07-04-2006 2:34 AM by RoyM. 2 replies.

Sort Posts:

  • Conversion of a BoundField.DataField

    07-03-2006, 4:04 AM
    • Member
      158 point Member
    • RoyM
    • Member since 10-31-2005, 10:42 AM
    • Trondheim, Norway
    • Posts 67

    I am creating a GridView dynamically, where the BoundFields is created like below:

    <code>
    Dim nameColumn As New BoundField()
    nameColumn.DataField = "myDblNumber"
    nameColumn.HeaderText = "The number"
    GridView1.Columns.Add(nameColumn)
    </code>

    How can I make the value in the gridview (in this case a number with decimals) red if the value is negative (less than zero)? I've tried the following, which gave an error (Conversion from string "myDblNumber" to type 'Double' is not valid):

    <code>
    Dim nameColumn As New BoundField()
    nameColumn.DataField = "myDblNumber"
    nameColumn.HeaderText = "The number"
    GridView1.Columns.Add(nameColumn)
    If nameColumn.DataField < 0 Then
       nameColumn.ItemStyle.CssClass = "errLabel"
    End If
    </code>

    Regards,
    Roy M. Halvorsen

  • Re: Conversion of a BoundField.DataField

    07-03-2006, 12:32 PM
    Answer
    • Member
      155 point Member
    • ryanr
    • Member since 01-06-2006, 9:06 PM
    • Posts 25

    You are in the right direction. But you would not do this at GridView creation time. You would do it for each item on the Row's Data Bound event. Otherwise you have no data to actually do the comparison on.

    So, e.g:

    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
      If e.Row.DataItem("myDblNumber") < 0 Then ' may have to first cast to a double
       e.Row.Cells(1).CssClass = "whatever" ' change 1 to your cell #
      End If
     End If
    End Sub

  • Re: Conversion of a BoundField.DataField

    07-04-2006, 2:34 AM
    • Member
      158 point Member
    • RoyM
    • Member since 10-31-2005, 10:42 AM
    • Trondheim, Norway
    • Posts 67
    Thanks a lot!!
    That was exactly what I was looking for, and it works great :-)

    Best regards,
    Roy
Page 1 of 1 (3 items)