I am working on a Windows Forms app, a problem has arisen in a DataGridView, I am working on the CellEndEdit event, in which when I edit a row and click on another row, it sends me an Exception.
The operation is not valid because it causes a reentrant call to the SetCurrentCellAddressCore function.
My code is as follows
private void dgvClasificacion_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
dgvClasificacion.Rows[e.RowIndex].ErrorText = string.Empty;
ClasificacionTalla item = new ClasificacionTalla();
DataGridViewRow row = dgvClasificacion.CurrentRow;
item.ClasificacionTallaId = Convert.ToInt32(row.Cells[0].Value);
item.Descripcion = Convert.ToString(row.Cells[1].Value);
if (!string.IsNullOrEmpty(item.Descripcion))
{
_repositoryClasificacionTalla.Create(item);
dgvClasificacion.AutoGenerateColumns = false;
listCT = _repositoryClasificacionTalla.GetAll().ToList(); => Traigo los datos desde la DB
dgvClasificacion.DataSource = null;
dgvClasificacion.Rows.Clear(); Borro los datos para que no se vuelvan a cargar los mismos, Es aquí el error.
foreach (var c in listCT) ==> Cargo el DGV con los datos de la lista listCT
{
dgvClasificacion.Rows.Add(c.ClasificacionTallaId, c.Descripcion);
}
}
else
dgvClasificacion.Rows.Remove(dgvClasificacion.Rows[dgvClasificacion.Rows.Count -1]);
}
As I add data to the DGV from the same control I load it by means of a ForEach, when creating and updating a new record I load the grid as the code shows, in the code line dgvClasificacion.Rows.Clear () ; That's where the exception is sent.
The error arises when I'm editing a row and I click on the other row, I hope you can help me, thanks in advance.
I found a supposed solution but it's not working for me but it's a code translator. DGV
But never enters the IF
public delegate void SetColumnIndex(int i);
private void dgvClasificacion_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
if ((dgvClasificacion.CurrentCell.ColumnIndex
!= (dgvClasificacion.Columns.Count - 1)))
{
int nextindex = Math.Min((dgvClasificacion.Columns.Count - 1), (dgvClasificacion.CurrentCell.ColumnIndex + 1));
SetColumnIndex method = new SetColumnIndex(Mymethod);
dgvClasificacion.BeginInvoke(method, nextindex);
dgvClasificacion.Rows[e.RowIndex].ErrorText = string.Empty;
ClasificacionTalla ct = new ClasificacionTalla();
DataGridViewRow fila = dgvClasificacion.CurrentRow;
ct.ClasificacionTallaId = Convert.ToInt32(fila.Cells[0].Value);
ct.Descripcion = Convert.ToString(fila.Cells[1].Value);
if (!string.IsNullOrEmpty(ct.Descripcion))
{
_repositoryClasificacionTalla.Create(ct);
dgvClasificacion.AutoGenerateColumns = false;
listCT = _repositoryClasificacionTalla.GetAll().ToList();
//dgvClasificacion.Rows.Clear();
foreach (var c in listCT)
{
dgvClasificacion.Rows.Add(c.ClasificacionTallaId, c.Descripcion);
}
}
else
dgvClasificacion.Rows.Remove(dgvClasificacion.Rows[dgvClasificacion.Rows.Count - 1]);
}
}
private void Mymethod(int columnIndex)
{
dgvClasificacion.CurrentCell = dgvClasificacion.CurrentRow.Cells[columnIndex];
dgvClasificacion.BeginEdit(true);
}