无法在列和行相同的位置更改单元格类型
Can't change cell type at location where column and row are same
private void Form1_Load(object sender, EventArgs e)
{
DataTable dt = new DataTable();
dt.Columns.Add("Col1");
dt.Columns.Add("Col2);
for (int j = 0; j < 10; j++)
{
dt.Rows.Add(new string[] { j.ToString(), "aaa"});
}
dataGridView1.DataSource = dt;
}
这里我创建了一个数据表并将其绑定到数据网格视图。我希望在单击时将 column = 1 和 row= 1 单元格更新为组合框单元格
所以我使用了以下事件。
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex == 1 && e.RowIndex == 1)
{
object value = dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value;
ComboBoxCell = new DataGridViewComboBoxCell();
ComboBoxCell.Items.AddRange(Values);
this.dataGridView1[1, 1] = ComboBoxCell;
this.dataGridView1[1, 1].Value = Values[0];
TextBoxCell = null;
}
}
private void dataGridView1_CellLeave(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex == 1 && e.RowIndex == 1)
{
TextBoxCell = new DataGridViewTextBoxCell();
this.dataGridView1[1, 1] = TextBoxCell;
ComboBoxCell = null;
}
}
但在 cell_leave 事件中引发了异常
"Operation is not valid because it results in a reentrant call to the
SetCurrentCellAddressCore function"
请帮我解决这个问题。
我尝试在 column=1 row = 2 上添加组合框然后它工作正常
但只有在同列同行的情况下才会出现问题
我想你可以使用 CellMouseLeave 事件。
private void dataGridView1_CellMouseLeave(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex == 1 && e.RowIndex == 1)
{
DataGridViewTextBoxCell TextBoxCell = new DataGridViewTextBoxCell();
this.dataGridView1[1, 1] = TextBoxCell;
}
}
我试过上面的代码,它工作正常。
我还想建议您阅读以下答案。
Why is my bound DataGridView throwing an "Operation not valid because it results in a reentrant call to the SetCurrentCellAddressCore function" error?
它并不完全像 CellLeave 事件那样工作,但它可能会帮助您找到一些解决方案。
private void Form1_Load(object sender, EventArgs e)
{
DataTable dt = new DataTable();
dt.Columns.Add("Col1");
dt.Columns.Add("Col2);
for (int j = 0; j < 10; j++)
{
dt.Rows.Add(new string[] { j.ToString(), "aaa"});
}
dataGridView1.DataSource = dt;
}
这里我创建了一个数据表并将其绑定到数据网格视图。我希望在单击时将 column = 1 和 row= 1 单元格更新为组合框单元格 所以我使用了以下事件。
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex == 1 && e.RowIndex == 1)
{
object value = dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value;
ComboBoxCell = new DataGridViewComboBoxCell();
ComboBoxCell.Items.AddRange(Values);
this.dataGridView1[1, 1] = ComboBoxCell;
this.dataGridView1[1, 1].Value = Values[0];
TextBoxCell = null;
}
}
private void dataGridView1_CellLeave(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex == 1 && e.RowIndex == 1)
{
TextBoxCell = new DataGridViewTextBoxCell();
this.dataGridView1[1, 1] = TextBoxCell;
ComboBoxCell = null;
}
}
但在 cell_leave 事件中引发了异常
"Operation is not valid because it results in a reentrant call to the SetCurrentCellAddressCore function"
请帮我解决这个问题。
我尝试在 column=1 row = 2 上添加组合框然后它工作正常 但只有在同列同行的情况下才会出现问题
我想你可以使用 CellMouseLeave 事件。
private void dataGridView1_CellMouseLeave(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex == 1 && e.RowIndex == 1)
{
DataGridViewTextBoxCell TextBoxCell = new DataGridViewTextBoxCell();
this.dataGridView1[1, 1] = TextBoxCell;
}
}
我试过上面的代码,它工作正常。
我还想建议您阅读以下答案。
Why is my bound DataGridView throwing an "Operation not valid because it results in a reentrant call to the SetCurrentCellAddressCore function" error?
它并不完全像 CellLeave 事件那样工作,但它可能会帮助您找到一些解决方案。