2012-02-20 107 views
0

我的DataGridView通過SqlDataReader綁定到數據庫查詢結果,當我測試單元格值爲null時,我得到意想不到的結果。檢查datagridview的單元格中的數據是否爲空

我的代碼是這樣的:

SqlConnection con = new SqlConnection(
    "Data Source = .;Initial Catalog = SAHS;integrated security = true"); 
con.Open(); 
SqlCommand cmd3 = new SqlCommand(
    "select Status from vw_stdtfeedetail 
    where Std= 6 and Div ='B' and name='bbkk' and mnthname ='June'", con); 
SqlDataReader dr = cmd3.ExecuteReader(); 
BindingSource bs = new BindingSource(); 
bs.DataSource = dr; 
dataGridView3.DataSource = bs; 
this.monthFeeTableAdapter.Fill(this.sAHSDataSet4.MonthFee); 
if (dataGridView3.CurrentCell.Value == null) 
{ 
    MessageBox.Show("Pending"); 
} 
else 
{ 
    MessageBox.Show("Already Paid"); 
} 

我得到的輸出已支付即使從數據庫中的值是

+1

當你調試,什麼是價值顯示爲? – peroija 2012-02-20 13:06:02

+0

@SingyJack - 這與OP所設想的完全相反。他假設NULL表示沒有付款。它可能是一個表示支付日期的列,因此NULL是指示沒有支付日期的完美方式。 – GarethD 2012-02-20 14:58:02

+0

是的,我已經明確了一個星期一的例子。 – StingyJack 2012-02-20 15:52:43

回答

3

嘗試使用的DBNull.Value代替null

if (dataGridView3.CurrentCell.Value == DBNull.Value) 
{ 
    MessageBox.Show("Pending"); 
} 
else 
{ 
    MessageBox.Show("Already Paid"); 
} 
+0

雅ive完成它..它正確使用DBNull.Value。 – HMcompfreak 2012-02-21 13:25:34

+0

@Hcompcompreak如果這是正確的答案,請將其標記爲正確的答案,將其從「未答覆」問題堆中移除。乾杯。 – GarethD 2012-02-22 00:41:06

相關問題