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?
source share