无法按索引访问 DataGridViewRow 单元格

Beh*_*ini 2 c# datagridview

我试图循环遍历 a 中的两列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];
    }
}
Run Code Online (Sandbox Code Playgroud)

但我遇到以下错误:

无法将 [] 索引应用于“System.Windows.Forms.DataGridViewRow”类型的表达式

您能告诉我为什么会发生这种情况吗?

问候,

Kon*_*ski 5

这非常简单 -DataGridViewRow类不公开索引器。您需要通过其Cells集合访问单元格。row.Cells[i]应该可以解决这个问题:

for (int i = 0; i < dataGridView1.Columns.Count; i++)
{
   currentFeature.DataRow[i] = row.Cells[i].Value as IConvertible;
}
Run Code Online (Sandbox Code Playgroud)