【问题标题】:How to check empty and null cells in datagridview using C#如何使用 C# 检查 datagridview 中的空单元格和空单元格
【发布时间】:2011-11-24 10:02:11
【问题描述】:

我正在尝试检查 datagridview 单元格中是否有空值和空值...但我做不到...

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

谁能帮帮我..这个...

【问题讨论】:

  • 你能告诉我你得到的错误信息是什么
  • 对于空/空检查使用string.IsNullOrEmpty()
  • 它没有显示任何错误消息,即使..我将单元格留空...

标签: c# winforms datagridview


【解决方案1】:

我会这样尝试:

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

【讨论】:

  • IsNullOrWhitespace 有错误说“字符串”不包含“IsNullOrWhitespace”的定义你能告诉我为什么吗?
  • “空白”中的“s”需要大写。
【解决方案2】:
if (String.IsNullOrEmpty(dataGridView1.Rows[i].Cells[3].Value as String))
{
    MessageBox.Show("cell is empty");
    return;
}

添加as String,它对我有用。

【讨论】:

    【解决方案3】:
    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;
            }
        }
    }
    

    【讨论】:

      【解决方案4】:

      我认为你应该使用这个:

      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();
      

      【讨论】:

        【解决方案5】:

        我认为你应该先检查 null

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

        【讨论】:

        • 你确定吗?这不是检查空值的常用方法,而且为什么要检查 FormattedValue 而不是仅检查 Value?
        • @David 建议 FormattedValue 在将任何特定格式应用于单元格后,发布这些比较值
        【解决方案6】:
        for (int i = 0; i < GV1.Rows.Count; i++)
        {
            if ((String)GV1.Rows[i].Cells[4].Value == null)
            {
                MessageBox.Show(" cell is empty");
                return;
            }
        }
        

        一切正常。

        【讨论】:

          【解决方案7】:

          试试这个:

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

          然后检查集合是否为空或有元素。

          我希望这会有所帮助。

          【讨论】:

            【解决方案8】:

            非常适合我:

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

            【讨论】:

              【解决方案9】:
                      // 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 ");
                              }
                          }
                      }
              

              【讨论】:

              • 请在您的回答中添加一些解释
              • 我已经尝试了这篇文章中提供的所有建议,但没有人适合我...最后我选择在设置值期间捕获任何类型的错误...如果有错误我跳过否则我设置值的单元格......我希望我的解释是详尽的。
              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多