GridView DataRowBound DBNULL issue


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!
 
Ad

Advertisements

E

Eliyahu Goldin

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
 
Ad

Advertisements

P

pvong

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!
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top