2011-04-29 90 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