How to reset the DataSource property of a DataGridView and not delete the columns of the Header?

2

I'm filling my DataGridView with a list of objects, however, when I fill in the grid for the first time everything is fine. When I remove the DataSource property as null, the grid columns are deleted.

Is it possible to reset the DataSource property without deleting the columns of the header?

Code snippet that I use:

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

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

When I reset the property within a method the header columns are deleted:

Code:

private void LimparCampos()
{          
    dgvControle.DataSource = null;
    dgvControle.Update();
    dgvControle.Refresh();
}
    
asked by anonymous 24.02.2017 / 15:45

1 answer

4

You can make an empty list so that it does not unmount its controle and remains with the same layout

private void LimparCampos()
{          
    dgvControle.DataSource = new List<Class>();
    dgvControle.Update();
    dgvControle.Refresh();
}

Where Class is the name of your class that loads this controle .

24.02.2017 / 16:49