2011-04-29 124 views
1

我正在查询datagridview,它工作得很好,除非其中一个单元格没有任何内容(dbnull)。如何过来这个?LINQ错误类型DBNull列

异常:未为'DBNull'类型定义运算符'='并键入'DBNull'。

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

回答

2

使用.Equals()方法来比较其中一个可能为空的值。例如:

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

或者,如果两者都可以是空,你可以使用基本Object.Equals()方法来比较:

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

@ user704430:这真是棒极了!谢谢。 – TroyS 2011-04-29 15:08:56

+0

为我解决了这个问题! – Mohgeroth 2011-06-16 15:48:22