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

标签 c# .net sql ado.net datagridview

我的 DataGridView 通过 SqlDataReader 绑定(bind)到数据库查询结果,当我测试 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

最佳答案

尝试使用 DBNull.Value 而不是 null

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

关于c# - 检查datagridview的单元格中的数据是否为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9361445/

相关文章:

.net - 远程服务器自动发现。广播与否?

mysql - 如何从其他模式表向mysql数据库插入值?

php - 从数据库中提取日期并仅选择今天的记录

c# - 不支持直接绑定(bind)到存储查询(DbSet、DbQuery、DbSqlQuery)的数据

c# - WCF Web 服务方法名称

c# - 如何实现接受匿名类型的 C# 扩展方法

.net - 找不到默认端点元素.NET 4.0

sql - 如何在 Python 3.7 中连接到 hadoop/hive 数据库(需要身份验证)并执行简单的 SQL 查询?

c# - 如何在 Winform 中更新 dataGridView 行的值

c# - .NET Google Calendar API 按日期排序事件