16

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...

PVitt
  • 11,500
  • 5
  • 51
  • 85
Vincent
  • 484
  • 4
  • 6
  • 21

9 Answers9

25

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...
    }
  } 
}
Davide Piras
  • 43,984
  • 10
  • 98
  • 147
6
if (String.IsNullOrEmpty(dataGridView1.Rows[i].Cells[3].Value as String))
{
    MessageBox.Show("cell is empty");
    return;
}

Add as String, it works for me.

Mystic Lin
  • 365
  • 4
  • 15
4

I think you should use this:

for (int i = 0; i < dataGridView1.RowCount; i++)
{
    for (int j = 0; j < dataGridView1.ColumnCount; j++) 
    {
        if (dataGridView1.Rows[i].Cells[j].Value == DBNull.Value)
        {
            dataGridView1.Rows[i].Cells[j].Value = "null";
        }
    }
}
dataGridView1.Update();
Deniz
  • 429
  • 1
  • 4
  • 19
Mujtaba
  • 85
  • 2
  • 10
4
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;
        }
    }
}
H Aßdøµ
  • 2,925
  • 4
  • 26
  • 37
IT Forward
  • 367
  • 2
  • 7
  • 28
1

I think you should check for null first

Convert.IsDBNull(dataGridView1.Rows[j].Cells[1].FormattedValue)

V4Vendetta
  • 37,194
  • 9
  • 78
  • 82
  • are you sure? this is not the usual way to check for nulls and also, why do you check against FormattedValue and not Value only? – Davide Piras Nov 24 '11 at 13:47
  • @David Suggested `FormattedValue` which would after any specific formats being applied to the cell, post those compare the values – V4Vendetta Nov 25 '11 at 04:34
0

Try this :

foreach (DataGridViewRow row in dataGridView.Rows)
{
    IEnumerable<DataGridViewCell> cellsWithValusInRows = from DataGridViewCell cell in row.Cells
        where string.IsNullOrEmpty((string)cell.Value)
        select cell;

     if (cellsWithValusInRows != null && cellsWithValusInRows.Any())
     {
         //Then cells with null or empty values where found  
     }
}

Then check for the collection if it is null or it has elements.

I hope this was helpful.

Deniz
  • 429
  • 1
  • 4
  • 19
Ali Ezzat Odeh
  • 2,093
  • 1
  • 17
  • 17
0
for (int i = 0; i < GV1.Rows.Count; i++)
{
    if ((String)GV1.Rows[i].Cells[4].Value == null)
    {
        MessageBox.Show(" cell is empty");
        return;
    }
}

It's working fine.

demongolem
  • 9,474
  • 36
  • 90
  • 105
0

Works perfect for me:

for (int i = dataGridView1.Rows.Count - 1; i >= 0; i--)
{
    DataGridViewRow dataGridViewRow = dataGridView1.Rows[i];

    foreach (DataGridViewCell cell in dataGridViewRow.Cells)
    {
        string val = cell.Value as string;
        if (string.IsNullOrEmpty(val))
        {
            if (string.IsNullOrEmpty(dataGridView1.Rows[i].Cells[3].Value as string)) // If you want to check more then just one cell you could also add "&& (string.IsNullOrEmpty(dataGridView1.Rows[i].Cells[ANY NUMBER].Value as string)
            {
                MessageBox.Show(" cell is empty");
                return;
                /* or to delete replace with:
                dataGridView1.Rows.Remove(dataGridViewRow);
                break;
                */
            }
        }
    }
}
Deniz
  • 429
  • 1
  • 4
  • 19
0
        // move cell value to Excel
        for (int i = 0; i < grd.Rows.Count; i++)
        {
            for (int t = 0; t < grd.Columns.Count; t++)
            {
                try
                {
                    worksheet.Cells[i + 2, t + 1] = grd.Rows[i].Cells[t].Value.ToString();
                }
                catch (Exception e)
                {
                    Debug.WriteLine(e.HResult + " " + e.Message + " I've always hated null ");
                }
            }
        }
  • Please add some explanation to your answer – Michael Rovinsky Jun 05 '21 at 18:16
  • I have tried all suggestions provided in this post and nobody works well for me... at end I have opted for catch any type of error during setting value... If there is an error I skip the cell otherwise I set the value... I hope my explanation is exaustive. – StephenSoftware Jun 09 '21 at 08:25