我的DataGridView通过SqlDataReader
绑定到数据库查询结果,当我测试null
的单元格值时,我得到了意外的结果。
我的代码是这样的:
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");
}
即使数据库中的值为 null ,我的输出已经付费。
答案 0 :(得分:4)
尝试使用DBNull.Value
代替null
if (dataGridView3.CurrentCell.Value == DBNull.Value)
{
MessageBox.Show("Pending");
}
else
{
MessageBox.Show("Already Paid");
}