我正在尝试遍历 DataGridView
中的两列并将它们添加到如下坐标中
foreach (DataGridView row in dataGridView1.Rows)
{
double x = Convert.ToDouble(row["X"]);
double y = Convert.ToDouble(row["Y"]);
Coordinate c = new Coordinate(x, y);
Point p = new Point(c);
IFeature currentFeature = fs.AddFeature(p);
for (int i = 0; i < dataGridView1.Columns.Count; i++)
{
currentFeature.DataRow[i] = row[i];
}
}
但我遇到了以下错误:
Cannot apply indexing with [] to an expression of type 'System.Windows.Forms.DataGridViewRow'
你能告诉我为什么会这样吗?
问候,
最佳答案
这很简单 - DataGridViewRow
类不公开索引器。您需要通过其 Cells
集合访问单元格。 row.Cells[i]
应该可以解决这个问题:
for (int i = 0; i < dataGridView1.Columns.Count; i++)
{
currentFeature.DataRow[i] = row.Cells[i].Value as IConvertible;
}
关于c# - 无法按索引访问 DataGridViewRow 单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11892421/