Error while deleting second List item

Asked

Viewed 85 times

2

I am using the following code to delete items:

if (e.KeyCode == Keys.Delete)
{
    var remover = listaPedido.SingleOrDefault(r => r.ItemOffline == int.Parse(gridPedido.CurrentRow.Cells[3].Value.ToString()));
    if (remover != null)
    {
        listaPedido.Remove(remover);
        gridPedido.DataSource = listaPedido;
    }   
}

The first time I delete an item, it works correctly. The second time gives Outofindex error (even though I have items in my list):

inserir a descrição da imagem aqui

Look through the screen behind the error, I still have 3 records on the datagrid.

What can it be?

  • 1

    If there are 3, you access from 0 to 2, in the code is 3.

  • in the code is the column I am using to make the comparison. it sees which row has the column itemOffline == column[3] of the grid

  • @bigown, too, the error that gives is in index 2. then it would have to work with the amount of items I have at the moment

  • I did another test here and the error always gives in the index of the amount of items I have in the table (in the case of the image appears 2 because I have three items, as you mentioned above)

  • 1

    You’ve tried it this way: gridPedido.CurrentRow.Value.ToString()?

  • What part of the @Acklay code? If it refers to the search for the record to delete, the system does it correctly, includes two messagebox and checks that it both finds the r.itemOffline correctly and finds the correct line as well. I imagine you have to give some sort of update on the grid :/

  • 1

    Try it this way: gridPedido.Update(); gridPedido.Refresh();

  • same error :/ tried with the two codes you suggested @Acklay. There is another suggestion to delete items from a list?

Show 3 more comments

2 answers

3


You have not posted the entire code, only the deletion snippet, but I will assume the following (based on the error that occurs):

  • Your grid starts with the variable listaPedido, which in turn should be a List<QualquerCoisa>;
  • At some point you initialize the list with the gridPedido.DataSource = listaPedido;
  • Note that when you delete an item from listaPedido, you reassociate that listaPedido to your gridPedido.DataSource, which in practice has no effect. Your grid already has the listaPedido as his DataSource;
  • The guy List<T> does not have built-in change notification events - in practice, your grid is unaware of the change and relies on the previous state rendering (when it still had 3 items), generating the error.

Solution

Simply package your data source into one BindingList<QualquerCoisa> (substitute QualquerCoisa by the real name of your class). Thus:

BindingList<QualquerCoisa> _bsListaPedido;  // Variável Global da Classe

void CarregaGrid() 
{
    _bsListaPedido = new BindingList<QualquerCoisa>(listaPedido);
    gridPedido.DataSource = _bsListaPedido;
}

When removing the item from the list, remove from Bindinglist:

_bsListaPedido.Remove(remover);

This will notify the Grid of the change accordingly and should go smoothly.

1

I managed to sort it out like this:

if (e.KeyCode == Keys.Delete)
{
    var remover = listaPedido.SingleOrDefault(r => r.ItemOffline == int.Parse(gridPedido.CurrentRow.Cells[3].Value.ToString()));
    if (remover != null)
    {
        listaPedido.Remove(remover);
        gridPedido.DataSource = null;
        gridPedido.DataSource = listaPedido;
    }   
}

adding the line gridPedido.DataSource = null; caused the dataGridView "zero" and refresh it again with the current list.

Browser other questions tagged

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