Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check empty and null cells in datagridview using C#

i am trying to check the datagridview cells for empty and null value... but i can not do it right...

for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
    if ((String)dataGridView1.Rows[i].Cells[3].Value == String.Empty)
    {
        MessageBox.Show(" cell is empty");
        return;
    }

    if ((String)dataGridView1.Rows[i].Cells[3].Value == "")
    {
        MessageBox.Show("cell in empty");
        return ;
    }
}

i even tried this codes

if (String.IsNullOrEmpty(dataGridView1.Rows[i].Cells[3].Value))
{
  MessageBox.Show("cell is empty");
  return;
}

can any one help me.. with this...

like image 993
Vincent Avatar asked Nov 24 '11 10:11

Vincent


People also ask

How check DataGridView is empty in C#?

You can find out if it is empty by checking the number of rows in the DataGridView. If myDataGridView. Rows. Count == 0 then your DataGridView is empty.

How check DataGridView is empty or not in VB net?

As for checking whether the DataGridView is empty: If there is no IsEmpty property you may have to look at other things. Maybe looking at RowCount and ColumnCount would help here. Show activity on this post. You can use DataGridView1.


3 Answers

I would try like this:

foreach (DataGridViewRow rw in this.dataGridView1.Rows)
{
  for (int i = 0; i < rw.Cells.Count; i++)
  {
    if (rw.Cells[i].Value == null || rw.Cells[i].Value == DBNull.Value || String.IsNullOrWhiteSpace(rw.Cells[i].Value.ToString())
    {
      // here is your message box...
    }
  } 
}
like image 140
Davide Piras Avatar answered Oct 22 '22 19:10

Davide Piras


if (String.IsNullOrEmpty(dataGridView1.Rows[i].Cells[3].Value as String))
{
    MessageBox.Show("cell is empty");
    return;
}

Add as String, it works for me.

like image 5
Mystic Lin Avatar answered Oct 22 '22 20:10

Mystic Lin


if (!GridView1.Rows[GridView1.CurrentCell.RowIndex].IsNewRow)
{
     foreach (DataGridViewCell cell in GridView1.Rows[GridView1.CurrentCell.RowIndex].Cells)
    {
        //here you must test for all and then return only if it is false
        if (cell.Value == System.DBNull.Value)
        {
            return false;
        }
    }
}
like image 4
IT Forward Avatar answered Oct 22 '22 18:10

IT Forward