How to delete datagridview line?

Asked

Viewed 1,272 times

2

Populo the datagridView thus:

dgvProdutosErp.DataSource = produtos; // produtos é uma lista

I try to remove as follows:

foreach (DataGridViewRow row in dgvProdutosErp.Rows)
{
    dgvProdutosErp.Rows.RemoveAt(row.Index);    
} 

The following error occurs:

It is not possible to programmatically remove lines unless Datagridview is associated with data in an Ibindinglist with change notification support and that allows deletion.

  • You cannot remove the item from datagridview, because it is associated with dataSource, you must delete the line in products and update the dgvProdutosErp, or not using the DataSource and fill in the datagridview on the same hand

  • Thank you Marco...

1 answer

2


The solution was as follows:

I populated differently and managed to exclude.

var bindingList = new BindingList<ProdutosModel>(produtos);
                var source = new BindingSource(bindingList, null);
                dgvProdutosErp.DataSource = source;

Excluding:

foreach (DataGridViewRow row in dgvProdutosErp.Rows)
{
    dgvProdutosErp.Rows.RemoveAt(row.Index);
}
  • I tried to use this your code, but here it is deleting more than one item at the same time. in yours just deletes the selected item?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.