Как проверить пустые и нулевые ячейки в datagridview, используя C # - PullRequest
13 голосов
/ 24 ноября 2011

я пытаюсь проверить ячейки таблицы данных на наличие пустых и нулевых значений ... но я не могу сделать это правильно ...

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 ;
    }
}

я даже пробовал эти коды

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

может ли кто-нибудь мне помочь .. с этим ...

Ответы [ 8 ]

15 голосов
/ 24 ноября 2011

Я бы попробовал так:

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...
    }
  } 
}
4 голосов
/ 08 марта 2015
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;
        }
    }
}
3 голосов
/ 04 сентября 2015
if (String.IsNullOrEmpty(dataGridView1.Rows[i].Cells[3].Value as String))
{
    MessageBox.Show("cell is empty");
    return;
}

Добавить as String, у меня это работает.

2 голосов
/ 29 июня 2012

Я думаю, вы должны использовать это:

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();
1 голос
/ 24 ноября 2011

Я думаю, вы должны сначала проверить на ноль

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

0 голосов
/ 04 июля 2018

Идеально подходит для меня:

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;
                */
            }
        }
    }
}
0 голосов
/ 13 марта 2017
for (int i = 0; i < GV1.Rows.Count; i++)
{
    if ((String)GV1.Rows[i].Cells[4].Value == null)
    {
        MessageBox.Show(" cell is empty");
        return;
    }
}

Работает нормально.

0 голосов
/ 08 ноября 2016

Попробуйте это:

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

Затем проверьте коллекцию, является ли она нулевой или имеет элементы.

Надеюсь, это было полезно.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...