c# datagridview第三行第三列这个cell,原来为combox,row.Cells[e.ColumnIndex] = textCell;提示SetCurrentCellAddressCore可重入调用 是什么原因
时间: 2024-04-15 16:27:02 浏览: 24
"SetCurrentCellAddressCore可重入调用" 错误通常是因为在 DataGridView 的事件处理程序中更改了当前单元格的位置,从而导致出现死循环。这可能是由于在更改单元格时触发了其他事件,导致重入调用。
要解决此问题,您可以尝试在更改单元格之前取消当前单元格的选择,以避免触发其他事件。您可以使用 `CurrentCell` 属性来取消当前选择,然后再进行单元格的更改。
以下是一个可能的解决方案:
```csharp
// 取消当前单元格的选择
dataGridView1.CurrentCell = null;
// 进行单元格的更改
DataGridViewTextBoxCell textCell = new DataGridViewTextBoxCell();
dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex] = textCell;
```
通过这样做,您可以避免触发其他事件并解决 "SetCurrentCellAddressCore可重入调用" 错误。
相关问题
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`。
dataInfo = new 用户基本信息(); for (int i = 0; i < dataGridView1.Rows.Count; i++) { if (dataGridView1.Rows[i].Selected == true) { dataInfo.No = yhbh.Text = dataGridView1.Rows[i].Cells[0].Value.ToString(); dataInfo.Name = yhmc.Text = dataGridView1.Rows[i].Cells[1].Value.ToString(); dataInfo.Password = yhmm.Text = dataGridView1.Rows[i].Cells[2].Value.ToString(); dataInfo.Dept = bm.Text = dataGridView1.Rows[i].Cells[3].Value.ToString(); dataInfo.Nation = zw.Text = dataGridView1.Rows[i].Cells[5].Value.ToString(); dataInfo.Job = textBox11.Text = dataGridView1.Rows[i].Cells[4].Value.ToString(); dataInfo.Birthday = dateTimePicker1.Text = dataGridView1.Rows[i].Cells[6].Value.ToString(); dataInfo.Phone = dh.Text = dataGridView1.Rows[i].Cells[7].Value.ToString(); dataInfo.Mail = yx.Text = dataGridView1.Rows[i].Cells[8].Value.ToString(); } }转化成不一样的代码
以下是可能的另一种实现方式:
```
if (dataGridView1.SelectedRows.Count > 0) // 检查是否有选中的行
{
DataGridViewRow row = dataGridView1.SelectedRows[0]; // 获取第一行
dataInfo = new 用户基本信息
{
No = yhbh.Text = row.Cells[0].Value.ToString(),
Name = yhmc.Text = row.Cells[1].Value.ToString(),
Password = yhmm.Text = row.Cells[2].Value.ToString(),
Dept = bm.Text = row.Cells[3].Value.ToString(),
Job = textBox11.Text = row.Cells[4].Value.ToString(),
Nation = zw.Text = row.Cells[5].Value.ToString(),
Birthday = dateTimePicker1.Text = row.Cells[6].Value.ToString(),
Phone = dh.Text = row.Cells[7].Value.ToString(),
Mail = yx.Text = row.Cells[8].Value.ToString()
};
}
```
该代码片段首先检查是否有选中的行,然后获取第一行,并将其数据赋值给 `dataInfo` 对象和窗体上的文本框。注意,这里使用了对象初始化器来构造 `dataInfo` 对象。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)