1
votes

I am querying a datagridview and it works great unless one of the cells has nothing (dbnull). How to over come this?

Exceptions: Operator '=' is not defined for type 'DBNull' and type 'DBNull'.

Dim query = From row As DataGridViewRow In DataGridView1.Rows _
            Where row.Cells(SelectedColumnIndex).Value = filter _
            And row.Visible = False _
            Select row Distinct
1

1 Answers

2
votes

Use the .Equals() method to compare values in which one may be null. Example:

Dim query = From row As DataGridViewRow In DataGridView1.Rows _
        Where row.Cells(SelectedColumnIndex).Value.Equals(filter) _
        And !(row.Visible) _
        Select row Distinct

Or if both may be null, you can use the base Object.Equals() method to compare:

Dim query = From row As DataGridViewRow In DataGridView1.Rows _
            Where Object.Equals(row.Cells(SelectedColumnIndex).Value, filter) _
            And !(row.Visible) _
            Select row Distinct