Flex Datagrid Visibility Bug

Run into a nasty little bug with Flex datagrids the other, under some situations Datagrid columns with their visibility set to false will be displayed!

Here's a workaround.

The issue occurred when setting an ArrayCollection that was bound to a datagrid the non visible columns reappeared.

The fix is to set the visibility to true and then back to false on each column. Not the most elegant solution but it works.

Note:This refers to a bug in the Flex 2 SDK not the Flex 3 SDK.

view plain print about
1// To handle visibility bug in datagrids
private function fixVisibility(dg:DataGrid):void {
3    var visible:Boolean = false;
5    for(var i:Number = 0; i < dg.columns.length; i++ ){
6        var column:DataGridColumn = dg.columns[i];
8        visible = column.visible;
9        column.visible = true;
10        column.visible = visible;
11    }

Comments (Comment Moderation is enabled. Your comment will not appear until approved.)
Damn! You rock, this has been plaguing me for some time. I had no idea it was a bug!

Thanks a ton!
# Posted By John W | 6/19/07 12:36 AM
Took me a while to work out the workaround. BTW Just call the above function when the ArrayCollection that the DataGrid is bound to changes.
# Posted By Justin Mclean | 6/19/07 8:34 AM
I found that the commitProperties of the DataGrid.as replaces visibleColumns
with columns array. I figure overriding this method and reseting
visibleColumns will solve the problem.

override protected function commitProperties():void
var visibleColumnstemp:Array = super.mx_internal::visibleColumns ; //workarround t
super.mx_internal::visibleColumns = visibleColumnstemp;
# Posted By Ahmad Hamid | 10/26/07 7:53 AM
Thanks for handy fix. I found using it like so proved to work pretty effectively:

<mx:DataGrid id='dg' updateComplete='fixVisibility(dg)' dataChange='fixVisibility(dg)' change='fixVisibility(dg)'>
# Posted By Dave | 5/9/08 7:03 AM
Thanks. Seems like this bug has found it's way into the new AdvancedDataGrid widget. This fix does the trick for it as well.
# Posted By Kevin | 12/17/09 8:42 AM