c# - 无法按索引访问 DataGridViewRow 单元格

标签 c# datagridview

我正在尝试遍历 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/

相关文章:

c# - 如何在C#中控制datagridview光标移动

VB.net DataGridView 更改 Cell.FormattedValue

c# - 将 datagridview 中的数据导出到 excel 工作表窗口窗体

c# - 如何过滤掉国家代码?

c# - 使用接口(interface)时的最佳实践

c# - 在程序执行期间更改枚举描述

c# - 意外的元组不等式

c# - 如何在 Xamarin iOS 上执行简单的后台任务

c#-4.0 - 如何将数据 Gridview 两列相乘并在另一列中显示结果

c# - 使用复选框更改 datagridview 中的tiny int