【问题标题】:Check whether data in cell of datagridview is null检查datagridview单元格中的数据是否为null
【发布时间】:2012-03-10 19:29:37
【问题描述】:

我的 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,我也会得到输出 Already Paid

【问题讨论】:

  • 调试时,值显示为什么?
  • @StingyJack - 这与 OP 的假设完全相反。他假设 NULL 表示未付款。它可以是一个指示支付日期的列,因此 NULL 是指示没有支付日期的完美方式。
  • 是的,我显然有一个星期一的案例。

标签: c# .net sql ado.net datagridview


【解决方案1】:

尝试使用DBNull.Value 而不是null

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

【讨论】:

  • @HMcompfreak 如果这是正确答案,请将其标记为正确答案,以便将其从“未回答”问题堆中删除。干杯。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-05-23
  • 2011-12-04
  • 2010-11-13
  • 1970-01-01
相关资源
最近更新 更多