Deleting the last row of a DataGridView

1

I have a DGV in which I enter data directly but when I create a new row but I precede the escape key is pq I do not want to enter data but there is a blank row created then that row I want to delete, this is my code. Creating row.

private void btnNuevoClasificacion_Click(object sender, EventArgs e)
    {
        if (dgvClasificacion.CurrentRow == null)
        {
            dgvClasificacion.Rows.Add();
            dgvClasificacion.CurrentRow.Cells[1].Selected = true;
            dgvClasificacion.BeginEdit(true);
        }
        else
        {
            dgvClasificacion.Rows[dgvClasificacion.Rows.Count - 1].Selected = true;
            dgvClasificacion.Rows.Add();
            DataGridViewRowPosition(dgvClasificacion);
        }  
    }

private void dgvClasificacion_CellEndEdit(object sender, DataGridViewCellEventArgs e)
    {
        dgvClasificacion.Rows[e.RowIndex].ErrorText = string.Empty;
        ClasificacionTalla item = new ClasificacionTalla();

        DataGridViewRow row = dgvClasificacion.CurrentRow;
        item.Descripcion = Convert.ToString(row.Cells[1].Value);
        if (!string.IsNullOrEmpty(item.Descripcion))
        {
            _repositoryClasificacionTalla.Create(item);
        }
        //else
        //    dgvClasificacion.Rows.Remove();

    }

The idea is that if this row is empty, remove it by removing it, I have to pass the index of the last row to eliminate it. Any suggestions?

    
asked by Pedro Ávila 02.05.2016 в 21:56
source

1 answer

2

That row shows the DataGridView by default to allow the user to enter new records, but if you want to visualize it, assign the property

DataGridView.AllowUserToAddRows Property

in false

> > > > when I add a new row but I do not decide to put any record is a blank row then I have to delete that row by code

you could use

DataGridView1.Rows.Remove(DataGridView1.Rows[DataGridView1.Rows.Count - 1]);
    
answered by 02.05.2016 / 22:18
source