0
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;
}

Here I have created one datatable and bind it to data grid view. I want update column = 1 and row= 1 cell as a combobox cell when it clicked so i used following events.

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;
    }
}

but here in cell_leave event it raised exceptions

"Operation is not valid because it results in a reentrant call to the SetCurrentCellAddressCore function"

Please help me to resolve this.

I tried to add combo box on column=1 row = 2 then its working fine but only when there is a same column and same row problem occurs

Nirav Kamani
  • 3,192
  • 7
  • 39
  • 68
Vimesh Shah
  • 129
  • 2
  • 15

1 Answers1

1

I think you can use the CellMouseLeave event.

private void dataGridView1_CellMouseLeave(object sender, DataGridViewCellEventArgs e)
{
    if (e.ColumnIndex == 1 && e.RowIndex == 1)
    {
        DataGridViewTextBoxCell TextBoxCell = new DataGridViewTextBoxCell();
        this.dataGridView1[1, 1] = TextBoxCell;
    }
}

I tried above code it's working fine.

And i would also like to suggest you to read following answer.

Why is my bound DataGridView throwing an "Operation not valid because it results in a reentrant call to the SetCurrentCellAddressCore function" error?

It is not exactly working as CellLeave event but it may help you to find some solution.

Community
  • 1
  • 1
Nirav Kamani
  • 3,192
  • 7
  • 39
  • 68