GridView DataRowBound DBNULL issue

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • pvong

    GridView DataRowBound DBNULL issue

    Doing this in VB.NET

    I have a gridview RowDataBound. I want to do a test on each row column
    (12). Some of the rows have NULL value.

    This is the error message I'm getting.
    Object cannot be cast from DBNull to other types.

    This is my code I'm using.
    -------------------------------
    If e.Row.Cells(12) Is System.DBNull.V alue = False Then
    PriceChange += Convert.ToDecim al(DataBinder.E val(e.Row.DataI tem,
    "PriceChang e"))
    If PriceChange < 0 Then
    e.Row.Cells(12) .ForeColor = Drawing.Color.R ed
    Else
    If PriceChange 0 Then
    e.Row.Cells(12) .ForeColor = Drawing.Color.G reen
    End If
    End If
    End If

    Can someone tell me what I'm doing wrong?



    Thanks!


  • Eliyahu Goldin

    #2
    Re: GridView DataRowBound DBNULL issue

    Row.Cells(12) is an object of class TableCell, it can't be DBNull.

    Instead do
    if DataBinder.Eval (e.Row.DataItem , "PriceChang e") isnot dbnull.value

    --
    Eliyahu Goldin,
    Software Developer
    Microsoft MVP [ASP.NET]



    "pvong" <phillip*at*yah oo*dot*comwrote in message
    news:%23eT32M3Q JHA.144@TK2MSFT NGP03.phx.gbl.. .
    Doing this in VB.NET
    >
    I have a gridview RowDataBound. I want to do a test on each row column
    (12). Some of the rows have NULL value.
    >
    This is the error message I'm getting.
    Object cannot be cast from DBNull to other types.
    >
    This is my code I'm using.
    -------------------------------
    If e.Row.Cells(12) Is System.DBNull.V alue = False Then
    PriceChange += Convert.ToDecim al(DataBinder.E val(e.Row.DataI tem,
    "PriceChang e"))
    If PriceChange < 0 Then
    e.Row.Cells(12) .ForeColor = Drawing.Color.R ed
    Else
    If PriceChange 0 Then
    e.Row.Cells(12) .ForeColor = Drawing.Color.G reen
    End If
    End If
    End If
    >
    Can someone tell me what I'm doing wrong?
    >
    >
    >
    Thanks!
    >
    >

    Comment

    • pvong

      #3
      Re: GridView DataRowBound DBNULL issue

      That was perfect. Thanks!



      "Eliyahu Goldin" <REMOVEALLCAPIT ALSeEgGoldDinN@ mMvVpPsS.orgwro te in
      message news:u9lHs93QJH A.4504@TK2MSFTN GP02.phx.gbl...
      Row.Cells(12) is an object of class TableCell, it can't be DBNull.
      >
      Instead do
      if DataBinder.Eval (e.Row.DataItem , "PriceChang e") isnot dbnull.value
      >
      --
      Eliyahu Goldin,
      Software Developer
      Microsoft MVP [ASP.NET]

      >
      >
      "pvong" <phillip*at*yah oo*dot*comwrote in message
      news:%23eT32M3Q JHA.144@TK2MSFT NGP03.phx.gbl.. .
      >Doing this in VB.NET
      >>
      >I have a gridview RowDataBound. I want to do a test on each row column
      >(12). Some of the rows have NULL value.
      >>
      >This is the error message I'm getting.
      >Object cannot be cast from DBNull to other types.
      >>
      >This is my code I'm using.
      >-------------------------------
      >If e.Row.Cells(12) Is System.DBNull.V alue = False Then
      >PriceChange += Convert.ToDecim al(DataBinder.E val(e.Row.DataI tem,
      >"PriceChange") )
      >If PriceChange < 0 Then
      >e.Row.Cells(12 ).ForeColor = Drawing.Color.R ed
      >Else
      >If PriceChange 0 Then
      >e.Row.Cells(12 ).ForeColor = Drawing.Color.G reen
      >End If
      >End If
      >End If
      >>
      >Can someone tell me what I'm doing wrong?
      >>
      >>
      >>
      >Thanks!
      >>
      >>
      >

      Comment

      Working...