Hiding a GridView Column

I was working on a dynamically bound GridView, to which some scenario’s had a column shown, and sometimes it was not shown. Unlike GridView’s that are statically databound on the page you cannot refer to “ReportGrid.Columns” and simply hide it…. this seems weird to me that this list is not populated…. oh well.

The solution is to set the visibility of each cell in the column:


This function above does exactly this. One element to not miss is that you will also need to hide any footer or header row cells as well.

This trick comes in handy when dynamically and programmatically formatting a GridView (dynamic columns, etc). You can do all your formatting as if the column is always present (since it is)… then last thing you do is hide the column.