Flex DataGrid Column Width

apache-flexcolumn-widthdatagrid

In my flex app I store the widths and visiblility of columns in an xml file. When the app loads it reads from the xml file and sets he columns values as applicable:

for(i = 0; i < columnsOrder.length; i++){
    newOrder[i] = myDG.columns[Number(columnsOrder[i]) - 1];
    newOrder[i].visible = (Number(columnsVisiblity[i]) == 1);
    newOrder[i].width = Number(columnsWidth[i]);
}
myDG.columns = newOrder;
myDG.invalidateList();

The problem appears to be setting the visibility (it sets the visible field correctly but messes up the width)… I've tried setting it after setting the width (outside of the loop) and before the loop as well. It resizes the columns properly if I don't do anything with the visibility.

Any ideas?

Best Answer

Add an import statement at the top of your class file:

import mx.core.mx_internal;

Then remove using the mx_internal namespace, remove the owner of the column, change the width and then reasign the parent:

public static function resizeColumn(col:DataGridColumn, size:int):void
    {
        var owner:* = col.mx_internal::owner
        col.mx_internal::owner = null;

        col.width = size;

        col.mx_internal::owner = owner;
    }

This ought to do the trick (well, it did for us after a couple of days of swearing)

Related Topic