I have a DataGridView cell that is of type System.Single.
if (myDataGridView.Rows[0].Cells[1].Value == null)
{
//some code
}
myDataGridView.Rows[0].Cells[1].Value
has value {}
. It's not null
, nor is it 0
, what should be on the right hand side of ==
to make the conditional true?
As noted, the value is an instance of DbNull
.
Given that DbNull.Value
is a singleton (and thus safe for reference comparisons), two options come to mind:
if (myDataGridView.Rows[0].Cells[1].Value == DBNull.Value)
or
if (myDataGridView.Rows[0].Cells[1].Value is DbNull)
Personally I quite like that latter - it fits in with the "is null" approach within a database, and it reads well... it makes it clearer that I'm interested in nullity as a sort of property rather than performing an actual equality comparison.
See more on this question at Stackoverflow