2012-02-20 109 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

相关问题