How to reset the Datasource property of a Datagridview and not delete the Header columns?

Asked

Viewed 359 times

2

I’m filling my Datagridview with a list of objects, but when I fill the grid for the first time it’s all right. When Seto the Datasource property is null, the grid columns are deleted.

There is the possibility to reset the Datasource property without deleting the header columns ?

Code snippet I use:

if (listVetObj.Count > 0)
{
    dgvControle.DataSource = listVetObj;
    dgvControle.Update();
    dgvControle.Refresh();

    txtQtdeLinhas.Text = dgvControle.RowCount.ToString();
    txtQtdeLinhas.Refresh();
}

When resetting the property within a method the header columns are deleted:

Code:

private void LimparCampos()
{          
    dgvControle.DataSource = null;
    dgvControle.Update();
    dgvControle.Refresh();
}
  • The header columns you create before correct ?

  • 1

    These are fixed on the grid. When I insert line to line, for performance reasons, I started using list and setando direct in Datasource, there began this problem.

1 answer

3


  • 1

    A detail, new List<T>() already creates a list, does not need the ToList() afterward.

  • The ToList was purposeful, I do not know the way he sends this List @jbueno stay quiet I know this by observing the question I do not know the real way he uses.

  • I’ll test you as you passed. And I’ll test you with and without Tolist();

  • 1

    @Virgilionovic, I did as you said and solved the problem, however, as the jbueno commented I did without Tolist(). Thank you

  • 1

    This @Diegofarias can test both ways if in your case it is an implentation of IList IListSource BindList if you don’t need to use the ToList if you don’t even need it! I purposely put.

  • @Diegofarias ready I formatted as you resolved.

  • 1

    @Virgilionovic thanks. Hug

Show 2 more comments

Browser other questions tagged

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