Delete a row in Datagrid

1

I'm trying to delete the line in which it has the focus of a datagrid , I've used the following code:

datagrid.Rows.RemoveAt(e.RowIndex);  

You are returning the following message:

  

Uncommitted new row can not be deleted.

What's wrong? Can anyone help me?

    
asked by anonymous 21.03.2015 / 15:41

3 answers

1

You must set the property AllowUserToAddRows from DataGridView to false .

And to avoid an exception OutOfRangeException , first check if the line index is valid.

var indice = e.RowIndex;
if (indice >= 0) {
    var linha = dataGridView1.Rows[indice];
    if (!linha.IsNewRow)
        dataGridView1.Rows.Remove(linha);
}
    
21.03.2015 / 16:17
0

You can do this:

private void btoDeleletarItem_Click(object sender, EventArgs e)
{
datagrid.Rows.RemoveAt(datagrid.CurrentRow.Index);
}

This way you are deleting from the DataGrid just the row that is selected in the Grid table. Remember that it is a good thing to check the SelectionMode option for FullRowSelect , so that when a row cell is selected by the user or you the entire row is checked. >     

23.05.2016 / 16:48
-1

Try this:

if (dgvItens.CurrentRow != null)
        {
            listaItens.RemoveAt(dgvItens.CurrentRow.Index);
            dgvItens.DataSource = listaItens.ToList();
        }
    
28.06.2017 / 17:00