简体   繁体   English

检查datagridview单元格中的数据是否为空

[英]Check whether data in cell of datagridview is null

My DataGridView is bound to a database query result via a SqlDataReader , and when I test a cell value for null , I'm getting unexpected results. 我的DataGridView通过SqlDataReader绑定到数据库查询结果,当我测试单元格值为null ,我得到了意外的结果。

My code is something like this: 我的代码是这样的:

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");
}

I'm getting output Already Paid even though the value from database is null . 即使数据库中的值为null,我仍将获得输出Already Paid

Try using DBNull.Value instead of null 尝试使用DBNull.Value而不是null

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM