Reload Datagridview in C#

Asked

Viewed 466 times

3

I am creating an event of a button in which it creates a Product object and adds in a list of Products and using this list to fill the Datasource of a dataGridView, but Datagridview 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();
}

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();
}

Complementation of Response

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

Browser other questions tagged

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