tags:

views:

231

answers:

2

I have a DataGridView form in my .NET application and I'd like to be able to show or hide additional columns based on a Boolean value at run time.

What's the best way to show/hide these columns in code (as opposed to in the Forms Designer)?

+4  A: 

The easiest option (if the columns are known ahead of time) is to add them as you would normally, then set Visible as appropriate (on each) at runtime.

You can also add extra columns completely at runtime (if you can't predict the schema) - but note that this might work differently depending on whether you are using data-binding (i.e. a DataSource).

Marc Gravell
+2  A: 

DataGridView contains a property Columns, which is a DataGridViewColumnCollection.

I believe in this collection, you can set which columns exist, and if they are visible.

For more info, start here: http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.columns.aspx

abelenky