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.
If there are 3, you access from 0 to 2, in the code is 3.
– Maniero
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
– Italo Rodrigo
@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
– Italo Rodrigo
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)
– Italo Rodrigo
You’ve tried it this way:
gridPedido.CurrentRow.Value.ToString()
?– viana
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 :/
– Italo Rodrigo
Try it this way: gridPedido.Update(); gridPedido.Refresh();
– viana
same error :/ tried with the two codes you suggested @Acklay. There is another suggestion to delete items from a list?
– Italo Rodrigo