DataGridView shows the hidden column during runtime

DataGridView shows the hidden column during runtime

Old forum URL: forums.lhotka.net/forums/t/5422.aspx


netdeveloper posted on Thursday, September 18, 2008

Hi,

While using the DataGridView with BindingSource object I faced problem that during runtime the grid was showing the column for which I set the visible property False during designtime. I spent enough time to find out the reason of this unexpected behaviour. Ultimately, I observed that if I set the DataSource property of the BindingSource of the grid in the Form Load event, this problem occurs, but when I do this in the Form's constructor, it works fine and the hidden column remains hidden.

Anyone faced this problem??

What is the difference in setting the Binding Source of the DataGridView in Form Load and Form Constructor events??

Regards

NetDeveloper

stefan replied on Thursday, September 18, 2008

Are you displaying the DataGridView inside a TabControl?
As far as I understood it, it is a feature of the TabControl to somehow 'lazyload' its tabs...
What I experienced was that the columndefinitions as defined at designtime, are not loaded
correctly when the control is bound to a source before it is displayed the first time.

After many hours of investigation I ended up doing the following:
- Never bind anything in the constructor, only InitializeComponents + setting up form event handlers
- In the Form_Load handler,
             first make sure all DataGridViews are 'painted' once,
             then set up the BindingSource

In the case of a TabControl I do the following (in Form_Load..):
    ' hiding invisible columns workaround
    Me.TabContainingGridView1.Show()
    Me.TabContainingGridView1.Hide()
    Me.TabContainingGridView2.Show()
    Me.TabContainingGridView2.Hide()
    Me.TabContainingGridView3.Show()
    Me.TabContainingGridView3.Hide()
    ...

This is not needed for the topmost Tab (displayed automatically).

This is the only solution I found on the web.
If anyone has a better idea, come on and show!
I really want to get rid of the above code snippet Smile [:)]

Stefan

netdeveloper replied on Saturday, September 20, 2008

You are right....Yes I am displaying the DataGridView inside a TabControl (3rd and 4th Tab).

For test purpose I moved a grid out of the TabControl, it worked as expected (the hidden columns remain hidden).

In your feedback you suggested to set the BindingSource in Form_Load, but my observation for the DataGridView is that if it is inside the TabControl and BindingSource is set in the Form_Load, it shows the hidden column. But if BindingSource is set in the Form's Constructor, it does not show the hidden columns.

Regards

NetDeveloper

simon_may replied on Saturday, September 20, 2008

I likewise have had the same problem with datagridviews displayed on tabcontrol. Interesting finding about when the BindingSource is set. My approach had been to handoe the DataBindingComplete event and set the column visible property to false, Not elegant I know.

Michael Hildner replied on Saturday, September 20, 2008

netdeveloper:
during runtime the grid was showing the column for which I set the visible property False during designtime

I've seen this too. Not sure if it applies in your case, but when I've seen it, it was always the first column in the grid (usually my PK) that I hide. I just rearrange the columns so the first column is a visible one.

Been a while, but I remember reading something about the grid automatically selects cell 0, 0, and in doing so, makes that column visible.

Copyright (c) Marimer LLC