DataGridViewRow 错误的单元格索引

DataGridViewRow Wrong Cell Index

我在尝试通过 DataGridViewRow 获取某些值时遇到问题。 问题是我的单元格索引是 7,当我尝试使用以下代码从单元格中获取数据时:

 foreach (DataGridViewRow r in this.mydatagrid.Rows)
        {
            if (Convert.ToBoolean(r.Cells[0].Value) == true)
            {
                DataGridViewComboBoxCell cc = (DataGridViewComboBoxCell)r.Cells[7];
                cc.Value = toolStripComboBox1.SelectedItem.ToString();
                edit_subject(Convert.ToInt32(r.Cells[1].Value), r.Cells[7].Value.ToString());
            }

一个异常错误说 Cells[7] 是 OutOfIndex 。 所以我尝试将单元格索引从 7 更改为 5 并且成功了 有时,有时没有。

我需要解释一下,因为它真的很奇怪。

好的,我找到了解决方案,问题是因为我的 SQL 语法(Datagrid 数据源)与 datagridview 列的顺序不同, 我没有得到它,因为数据网格自动填充已被禁用。 并且我已经手动为每一列设置了 DataPropertyName。

无论如何,感谢您的回复。