Why don't the columns remain hidden after calling DataGridView.Columns.Clear ()?

I saw a lot of posts about SO about the strange behavior of the columns and their visibility, in particular, when updating the grid and dynamically building the columns in the list, but did not find a satisfactory solution.

After some digging, I'm pretty sure that this problem is related to using the DataGridView.Columns.Clear() method.

So far, I have not been able to decide why, but by removing the Clear () method, when I dynamically create my DataGridView columns, hidden columns stop appearing, but I do not understand why this would affect this? Of course, if you clear the collection of columns and use DataGridView.Columns.Add() to start adding new ones, for example:

 dataGridView1.Columns.Clear(); // This is the offending method!! dataGridView1.AutoGenerateColumns = false; dataGridView1.ShowEditingIcon = false; dataGridView1.RowHeadersVisible = false; DataGridViewTextBoxColumn col = new DataGridViewTextBoxColumn(); col.DataPropertyName = "ID"; col.HeaderText = "ID"; col.Visible = false; // Notice the visibility of this column... dataGridView1.Columns.Add(col); ... // Code is repeated for other columns in the collection 

I do not see anything wrong, but if dataGridView1.Columns.Clear(); enabled at the beginning, my hidden column becomes visible, is this a mistake?

+4
source share
2 answers

I was able to reproduce the problem. The Clear method call is fine, you can delete columns one by one, and the problem still persists. The "abusive" call is surprisingly Add :

 col.Visible = false; // Notice the visibility of this column... if (col.Visible) { // Just to be sure. Never get here. } dataGridView1.Columns.Add(col); if (col.Visible) { // Surprise! We are here. } 

Why is this happening?

This is definitely a mistake. The problem occurs only and only if all the following conditions are true:

  • DataGridView is in bound mode, i.e. has a DataSource . The type of data source does not matter.
  • Columns collection is empty
  • Add is called with a column with Visible = false

In this case, the code falls into the internal method DataGridViewDataConnection MatchCurrencyManagerPosition . Take a look at the source code , especially

 // Treat case where columnIndex == -1. We change the visibility of the first column. 

and a block of code after this comment.

How to avoid it

To repeat, this only happens in data binding mode and only for the first added column if it is set as hidden.

So, there are several ways to fix this:

  • Make sure the grid is not in linked mode when re-arranging columns

     var dataSource = dataGridView.DataSource; dataGridView.DataSource = null; // Repopulate columns //... dataGridView.DataSource = dataSource; 
  • Do not use the Add method. Create all the columns and save them in variables or a temporary list, and in the end use the AddRange method, which has no such effect.

  • Do not set Visible = false in advance. Create and add all the columns, then hide the columns you need.

+3
source

This is because the datatable DefaultView is directly set as the gridview data source.

We must set the datasource property to DefaultView.ToTable DefaultView.ToTable() , because whenever the datatable is Clearated or Reset, it clears the metadata and therefore loses information about the visibility of the grid.

Now it’s really unrecognized why the reselling information related to the data affects the visibility of the DataGridViewColumn .

0
source

Source: https://habr.com/ru/post/1446943/


All Articles