3

How can I check the null cell value in a DataGridView in C#? I've used DBNull for checking, but it is not working.

Can anybody help me?

The code I currently have is:

string[] ar=new string[dataGridView1.Columns.Count];
for(int i=0;i<dataGridView1.Columns.Count;i++)
{
   if (dataGridView1.Rows[0].Cells[i].Value != DBNull.Value)
   {
     if (i != 0)
    {
       ar[i] = dataGridView1.Rows[0].Cells[i].Value.ToString ();
    }
    else
    {
       ar[i] = dataGridView1.Rows[0].Cells[i].Value.ToString();
    }
}
Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
priyanka
  • 543
  • 4
  • 10
  • 18

1 Answers1

4

you can use this

if(!Convert.IsDBNull(dataGridView1.Rows[0].Cells[i].Value))
{
    if (i != 0)
    {
       ar[i] = dataGridView1.Rows[0].Cells[i].Value.ToString ();
    }
    else
    {
       ar[i] = dataGridView1.Rows[0].Cells[i].Value.ToString();
    }
}                                                     
Rémi
  • 3,867
  • 5
  • 28
  • 44
Deepak
  • 7,507
  • 3
  • 24
  • 26