GridView DataRowBound DBNULL issue

  • Thread starter Thread starter pvong
  • Start date Start date
P

pvong

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.Value = False Then
PriceChange += Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem,
"PriceChange"))
If PriceChange < 0 Then
e.Row.Cells(12).ForeColor = Drawing.Color.Red
Else
If PriceChange > 0 Then
e.Row.Cells(12).ForeColor = Drawing.Color.Green
End If
End If
End If

Can someone tell me what I'm doing wrong?



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

Instead do
if DataBinder.Eval(e.Row.DataItem, "PriceChange") isnot dbnull.value
 
That was perfect. Thanks!



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

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

--
Eliyahu Goldin,
Software Developer
Microsoft MVP [ASP.NET]
http://msmvps.com/blogs/egoldin


pvong said:
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.Value = False Then
PriceChange += Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem,
"PriceChange"))
If PriceChange < 0 Then
e.Row.Cells(12).ForeColor = Drawing.Color.Red
Else
If PriceChange > 0 Then
e.Row.Cells(12).ForeColor = Drawing.Color.Green
End If
End If
End If

Can someone tell me what I'm doing wrong?



Thanks!
 
Back
Top