简体   繁体   中英

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.

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 .

Try using DBNull.Value instead of null

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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