Items are not shown in dataGridView

Asked

Viewed 31 times

1

I am using the following code to include items in a list and show them in mine 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 code of the button that includes items in the list and passes to DataGridView. What happens is that the first time it works (the first item is shown inside the DataGridView), but from the second, it doesn’t work (keeps showing only the first). Included the MessageBox to confirm that items are being included in the list and confirmed that they are.

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

  • pedido is what?

  • request is the list

  • 1

    writes like this gridPedido.DataSource = pedido.ToList()

  • @Virgilionovic worked perfectly! Thanks. If you want, post the answer for me to score. Hug.

1 answer

2


Change this:

gridPedido.DataSource = pedido;

therefore:

gridPedido.DataSource = pedido.ToList();

forcing the type of data sent to DataSource of DataGridView List<T>. That one 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


References:

Browser other questions tagged

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