c# datagridview第三行第三列这个cell,原来为combox,row.Cells[e.ColumnIndex] = textCell;提示SetCurrentCellAddressCore可重入调用 是什么原因
时间: 2024-04-15 07:27:02 浏览: 132
"SetCurrentCellAddressCore可重入调用" 错误通常是因为在 DataGridView 的事件处理程序中更改了当前单元格的位置,从而导致出现死循环。这可能是由于在更改单元格时触发了其他事件,导致重入调用。
要解决此问题,您可以尝试在更改单元格之前取消当前单元格的选择,以避免触发其他事件。您可以使用 `CurrentCell` 属性来取消当前选择,然后再进行单元格的更改。
以下是一个可能的解决方案:
```csharp
// 取消当前单元格的选择
dataGridView1.CurrentCell = null;
// 进行单元格的更改
DataGridViewTextBoxCell textCell = new DataGridViewTextBoxCell();
dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex] = textCell;
```
通过这样做,您可以避免触发其他事件并解决 "SetCurrentCellAddressCore可重入调用" 错误。
相关问题
DataGridViewHeaderCell headerCell = dataGridView.HeaderRow.Cells[e.ColumnIndex]; 提示未包含HeaderRow
看起来你在尝试访问`dataGridView`的数据网格视图的头部行(HeaderRow)时遇到了问题,提示"未包含HeaderRow"。这可能是因为你忘记在创建控件或加载数据之前设置了它的`DataSource`,或者在某些情况下,如动态添加列后,需要手动初始化`HeaderRow`。
在使用前,请确保已经设置了`DataSource`并配置了适当的列。例如,如果你使用的是Windows Forms项目,可以在窗体加载完成后添加以下代码:
```csharp
dataGridView.DataSource = yourDataSetOrDataTable; // 将你的数据源替换到这里
dataGridView.RowHeadersVisible = true; // 显示列头行
// 初始化列标题
if (!dataGridView.Columns.Any())
{
foreach (DataColumn column in yourDataSet.Columns) // 用实际的数据列替换这里
{
dataGridView.Columns.Add(column.ColumnName, column.HeaderText);
}
}
```
确保在初次访问`HeaderRow`之前完成了这些步骤。然后再尝试获取`headerCell`应该就没问题了。
dataInfo = new DeviceManage.设备采购(); foreach (DataGridViewRow row in dataGridView1.Rows) { if (row.Selected == true) { if (isadmin == "1") { if (row.Cells[0].Value == null) { dataInfo.buy_no = cgdh.Text = dataGridView1.Rows[i].Cells[2].Value.ToString(); dataInfo.login_no = yhbh.Text = dataGridView1.Rows[i].Cells[3].Value.ToString(); dataInfo.dev_name = xbmc.Text = dataGridView1.Rows[i].Cells[4].Value.ToString(); dataInfo.dev_model = xbxh.Text = dataGridView1.Rows[i].Cells[5].Value.ToString(); dataInfo.gys_name = gys.Text = dataGridView1.Rows[i].Cells[6].Value.ToString(); dataInfo.buy_date = dateTimePicker1.Text = dataGridView1.Rows[i].Cells[7].Value.ToString(); dataInfo.price = dj.Text = dataGridView1.Rows[i].Cells[8].Value.ToString(); dataInfo.num = sl.Text = dataGridView1.Rows[i].Cells[9].Value.ToString(); }
将上述代码中的 `for` 循环改为 `foreach` 循环,并将代码中的索引变量 `i` 替换为 `row.Index`,可以使用以下代码:
```csharp
dataInfo = new DeviceManage.设备采购();
foreach (DataGridViewRow row in dataGridView1.Rows)
{
if (row.Selected == true)
{
if (isadmin == "1")
{
if (row.Cells[0].Value == null)
{
dataInfo.buy_no = cgdh.Text = row.Cells[2].Value.ToString();
dataInfo.login_no = yhbh.Text = row.Cells[3].Value.ToString();
dataInfo.dev_name = xbmc.Text = row.Cells[4].Value.ToString();
dataInfo.dev_model = xbxh.Text = row.Cells[5].Value.ToString();
dataInfo.gys_name = gys.Text = row.Cells[6].Value.ToString();
dataInfo.buy_date = dateTimePicker1.Text = row.Cells[7].Value.ToString();
dataInfo.price = dj.Text = row.Cells[8].Value.ToString();
dataInfo.num = sl.Text = row.Cells[9].Value.ToString();
}
}
}
}
```
在 `foreach` 循环中,`row` 直接表示集合中的每个元素,可以直接使用 `row.Cells[0]` 访问该行中的第一列单元格。使用 `row.Index` 可以获取当前行的索引。注意,`foreach` 循环中的变量是只读的,不能修改它们的值,因此需要使用 `row.Index` 来代替原来的索引变量 `i`。
阅读全文
相关推荐
















