How can I check the null cell value in the DataGridView?

19,971

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();
    }
}                                                     
Share:
19,971

Related videos on Youtube

priyanka
Author by

priyanka

Updated on June 04, 2022

Comments

  • priyanka
    priyanka almost 2 years

    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();
        }
    }
    
    • Bala R
      Bala R about 13 years
      when you say you have tried DBNull, did you try something like if (dataGridView1.Rows[j].Cells[1].Value == DBNull.Value) ?
    • priyanka
      priyanka about 13 years
      thanx for ur help but i've already used this condition but it is not working correctly. But i got my answer of this question
    • Cody Gray
      Cody Gray about 13 years
      Please post the code that you've tried so far. Explain to us how it's "not working". What result did you get, compared to what result did you expect?
    • priyanka
      priyanka about 13 years
      i don't know how to attach code ? can u tell me?
    • Cody Gray
      Cody Gray about 13 years
      I formatted the code for you already. You just indent each line by 4 spaces, or select the block and click the {} on the toolbar.
  • Rémi
    Rémi over 10 years
    The correcte function name is Convert.IsDBNull I will edit to fix the typo.