c# - 如果在DataGridView单元格上存在

标签 c# datagridview error-handling

我有一个DataGridView,我想遍历DataGridView中的所有单元格,以捕获周围单元格的值。我在DataGridView的边缘上得到了预期的超出范围的错误,但是我无法找到一种方法来首先测试它们的存在?

在下面,我将周围单元的选定状态存储到 bool(boolean) 数组中,但是该单元不存在,那么我将如何测试或处理呢?
neighbourCells[0] = dgv_Current.Rows[x-1].Cells[y].Selected;
任何帮助,不胜感激。

最佳答案

我猜您可能使这个问题复杂化了。你的评论…

”but I can't find a way for testing their existence first”



如果“不存在”,很难测试是否存在!这就是为什么如果不检查单元格周围的这些“不存在”的单元格,则会收到Out Of Range错误的原因。显然,任何边缘单元都将属于此类。例如,对于任何第一列单元格,它将包含三(3)个“不存在”单元格左上(TL),左下(BL)和左(L)。最后一列TR,BR和R同样适用。

如果我们尝试获取它们的值,则这些单元格将超出“范围”……检查这些单元格并返回类似“OOR”的值非常重要,这意味着周围的单元格“不存在”。

我建议一种简单的方法,该方法采用单元格行和col索引,并返回显示周围单元格值的字符串列表。对于“不存在”的单元格,字符串“OOR”(超出范围)用于表示周围不存在的单元格。

为了简化此过程,我建议第二种方法,该方法给定行和列的索引,它将仅返回单元格的值。在这种方法中,确保给定的行和列索引在网格的行和列范围内是很简单的。可能看起来像下面的样子……
private string GetCellValue(int row, int col) {
  if (row >= 0 && row < dataGridView1.Rows.Count && col >= 0 && col < dataGridView1.Columns.Count) {
    if (dataGridView1.Rows[row].Cells[col].Value != null)
      return dataGridView1.Rows[row].Cells[col].Value.ToString();
    else
      return "null";
  }
  else {
    return "OOR";
  }
}

首先是明显的检查;如果行索引大于或等于0,并且行索引也小于网格中的总行数。列索引也是如此。如果行索引或col索引均未通过这些测试……我们知道它是“不存在”的单元格,只需返回类似“OOR”的内容。如果行索引和列索引有效,那么我们仅返回单元格值,显然检查像新行一样的空单元格。

使用这种方法,应该很容易获得网格中任何单元的周围值。下面,使用List<string>收集周围的单元格值。给定一个单元格的行和列索引,它将返回一个字符串列表,这样每个字符串都将列出一个周围的单元格,左上角单元格的示例“TL->单元格值”,“TR->单元格值”,“T->调用值”等。这意味着我们需要调用八(8)次以上的方法,一次用于TL,TR,T,B,L,R,BL,BR。将返回的字符串添加到周围的单元格列表中。它可能看起来像下面的样子。
private List<string> GetSurroundingCells(int row, int col) {
   List<string> surroundingCells = new List<string>();
   surroundingCells.Add("T->" + GetCellValue(row - 1, col));
   surroundingCells.Add("TL->" + GetCellValue(row - 1, col - 1));
   surroundingCells.Add("TR->" + GetCellValue(row - 1, col + 1));
   surroundingCells.Add("B->" + GetCellValue(row + 1, col));
   surroundingCells.Add("BL->" + GetCellValue(row + 1, col - 1));
   surroundingCells.Add("BR->" + GetCellValue(row + 1, col + 1));
   surroundingCells.Add("L->" + GetCellValue(row, col - 1));
   surroundingCells.Add("R->" + GetCellValue(row, col + 1));
   return surroundingCells;
}

为了进行测试,将具有三(3)个文本列的DataGridView和多行TextBox拖放到表单上。网格SelectionChanged事件已连接,并且在触发时将使用网格CurrentCell在文本框中列出周围的单元格值。我希望这有帮助。
private void Form1_Load(object sender, EventArgs e) {
  for (int i = 0; i < 10; i++) {
    dataGridView1.Rows.Add("C0R" + i, "C1R" + i, "C2R" + i);
  }
}

private void dataGridView1_SelectionChanged(object sender, EventArgs e) {
  int row = dataGridView1.CurrentCell.RowIndex;
  int col = dataGridView1.CurrentCell.ColumnIndex;
  List<string> surroundingCellsList = GetSurroundingCells(row, col);
  textBox1.Text = "--- Cell a row: " + row + " col: " + col + " Value: " + GetCellValue(row, col) + Environment.NewLine;
  foreach (string item in surroundingCellsList) {
    textBox1.Text += item + Environment.NewLine;
  }

关于c# - 如果在DataGridView单元格上存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54550853/

相关文章:

C# DataGridView-如何启用垂直滚动条

C# DataGrid 更新所有行

android - flutter 任务 ':app:checkDebugAarMetadata' 执行失败

c# - 从控制台获取 C# 输入但不返回正确的输出

c# - 无法将 Json 解析为 .NET 类

c# - Visual Studio 2010 不会自动缩进行

c - 我应该检查相对误差或相对误差百分比吗?

c# - 复制到 Dropbox 文件夹时修改 LastWriteTime

c# - 将 TimeSpan 绑定(bind)到 DataGridView 列

error-handling - Mendix错误处理中的自定义错误