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.

// To handle visibility bug in datagrids
private function fixVisibility(dg:DataGrid):void {
   var visible:Boolean = false;
   
   for(var i:Number = 0; i < dg.columns.length; i++ ){
      var column:DataGridColumn = dg.columns[i];
      
      visible = column.visible;
      column.visible = true;
      column.visible = visible;
   }
}

Comments (Comment Moderation is enabled. Your comment will not appear until approved.)
John W's Gravatar 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
Justin Mclean's Gravatar 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
Ahmad Hamid's Gravatar 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.
eg.

override protected function commitProperties():void
{
var visibleColumnstemp:Array = super.mx_internal::visibleColumns ; //workarround t
super.commitProperties();
super.mx_internal::visibleColumns = visibleColumnstemp;
}
# Posted By Ahmad Hamid | 10/26/07 7:53 AM
Dave's Gravatar 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
Copyright © Justin Mclean 2008
BlogCFC by Raymond Camden.