Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/redis/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何使用C检查datagridview中的空单元格和空单元格_C#_Winforms_Datagridview - Fatal编程技术网

C# 如何使用C检查datagridview中的空单元格和空单元格

C# 如何使用C检查datagridview中的空单元格和空单元格,c#,winforms,datagridview,C#,Winforms,Datagridview,我正在尝试检查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 ((Strin

我正在尝试检查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 ;
    }
}
有人能帮我吗。。有了这个…

我想这样做:

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

我想你应该先检查一下空值


Convert.IsDBNulldataGridView1.Rows[j].Cells[1].FormattedValue

我认为您应该使用这个:

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();
添加为字符串,对我有效。

尝试以下操作:

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  
     }
}
然后检查集合是否为null或是否包含元素

我希望这是有帮助的

for (int i = 0; i < GV1.Rows.Count; i++)
{
    if ((String)GV1.Rows[i].Cells[4].Value == null)
    {
        MessageBox.Show(" cell is empty");
        return;
    }
}
它很好用。

非常适合我:

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

您能告诉我,对于空/空检查use string.IsNullOrEmptyit,您收到的错误消息是什么吗?即使。。我把手机留空了…你确定吗?这不是检查空值的常用方法,而且,为什么只检查FormattedValue而不检查Value?@David建议使用FormattedValue,在对单元格应用任何特定格式后,张贴那些比较值的帖子。Null或Whitespace有错误,说“string”不包含“IsNull或Whitespace”的定义。你能告诉我为什么吗?空格中的s需要大写。请给你的答案添加一些解释。我已经尝试了这篇帖子中提供的所有建议,没有人适合我。。。最后,我选择在设置值期间捕获任何类型的错误。。。如果有错误,我跳过单元格,否则我设置值。。。我希望我的解释是正确的。
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  
     }
}
for (int i = 0; i < GV1.Rows.Count; i++)
{
    if ((String)GV1.Rows[i].Cells[4].Value == null)
    {
        MessageBox.Show(" cell is empty");
        return;
    }
}
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;
                */
            }
        }
    }
}
        // 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 ");
                }
            }
        }