Items are not shown in the dataGridView

1

I'm using the following code to include items in a list and show the same in my DataGridView :

PedidodetalheOffline item = new PedidodetalheOffline(); //crio um novo item
item.IdOffline = id; //seto o id
item.PedidoOffline = numero; //seto o numero do pedido
item.ItemOffline = iditem; //seto o item
pedido.Add(item); //adiciono o item à lista "pedido"
id++; //incremento a variavel id para nao repetir

MessageBox.Show(pedido.Count().ToString()); //quantidade de intens que tem na lista
gridPedido.DataSource = pedido; //passo a lista pro DataGridView

This is the button code that includes items in the list and passes to DataGridView . What happens is that the first time it works (the first item is shown within DataGridView ), but from the second, it does not work (it continues to show only the first). I've included MessageBox to confirm that the items are being added to the list and have confirmed that they are.

What can it be? Do you have any commands to update the list or DataGridView each time I add a new item?

    
asked by anonymous 02.01.2017 / 02:22

1 answer

2

Change this:

gridPedido.DataSource = pedido;

why:

gridPedido.DataSource = pedido.ToList();

forcing the datatype sent to DataSource of DataGridView List<T> . This DataSource can receive the implemented types of interfaces:

  • IList and IList<T> ,
  • IListSource (Example: DataTable and DataSet ),
  • IBindingList (Example: BindingList<T> class) and
  • IBindingListView (Example: BindingSource class).

Reference

02.01.2017 / 02:38