Reload DataGridView in C #

3

I'm creating an event from a button in which it creates a Product object and adds it to a list of Products and using this list to populate the DataSource of a dataGridView, but the DataGridView always keeps appearing a single product.

private void button3_Click(object sender, EventArgs e)
{
      Produto produto = new Produto();

      venda.ItensVenda.Add(produto);

      dataGridView1.DataSource = venda.ItensVenda;

      dataGridView1.Refresh();
}
    
asked by anonymous 09.01.2017 / 18:43

1 answer

5

The reference to datasource still continues, even after another click. Force cleanup for a successful upgrade.

Add the line:

  

dataGridView1.DataSource = "";

private void button3_Click(object sender, EventArgs e)
{
      Produto produto = new Produto();

      venda.ItensVenda.Add(produto);

      dataGridView1.DataSource = "";
      dataGridView1.DataSource = venda.ItensVenda;

      dataGridView1.Refresh();
}

Response Supplementation

Another way to do this is to use BindingList in the creation of its object ItensVenda . In this way, the data will be bound automatically by dispensing with DataSource = "" .

    
09.01.2017 / 19:09