Tuesday, May 27, 2008

Dynamic (Advanced)DataGrid columns

Trying to add columns dynamically to your DataGrid or AdvancedDataGrid and having trouble with specifically setting the width of a column?

Try the following:

var cols: Array = datagrid.columns;
cols.push(newColumn);
datagrid.columns = cols;
// The following line is very important
// (otherwise the grid won't be aware of the new columns yet)
datagrid.validateNow();
// Force the width of a specific column
cols[0].width = 40;

25 comments:

Anonymous said...

Exactly what I was looking for. Thanks for the quick tip!

Anonymous said...

Thanks, save lot of time and nerves!

Palmero Tom said...

I'm happy that I could help!

Anonymous said...

Is there any way to add the new column as the first column in the datagrid? Thanks!

Palmero Tom said...

The easiest solution is to create a new array that holds the new column as its first item and the other columns would be the other items in the array. After 'preparing' the array you can assign it to the columns property of the datagrid.

Anonymous said...

Thank you for the solution to have the new column be the first!

Vidya said...

this helped me a lot. thanks for sharing your knowledge.

Anonymous said...

Awesome help - just when I was pulling my hair out. Thanks!

zeno said...

do you know how to set the namespace for the dataField for this dynamically added column?
Thanks a bunch?
Aman

Palmero Tom said...

Which namespace are you referring to? If you use Flexbuilder then the namespace import statement should be auto-generated...

Anonymous said...

Thanks. Do you also know how to force all the columns in an AdvancedDataGrid to resize after adding a new column?

In my application, the user can add many new columns to the ADG. As they add more columns, the grid starts to look out of proportion. However, if they close and reopen the ADG, it looks great. How can I force this resize without closing and reopening the ADG?

cheers!

Unknown said...

The following loops thru all columns and sets to same width.
Where adg = AdvancedDataGrid

private function addNewColumn(text:String,field:String):void
{
var cols:Array = adg.columns;
var col:AdvancedDataGridColumn = new AdvancedDataGridColumn();
col.headerText = text;
col.dataField = field;
cols.push(col);
adg.columns = cols;
adg.validateNow();
for ( var iCol:int = 0; iCol < cols.length; iCol++ )
cols[iCol].width = 250;
}

Unknown said...

Hi, I am knew to Flex... Can any one help me on how to provide dynamic columns to the advanced data grid as i am getting column count and headers from result event(from backend)..

Palmero Tom said...

Simply instantiate (Advanced)DataGridColumn objects, put them in an array and assign the array to the columns property of the grid.

Sunit Waingankar said...

Thanx for this Excellent article.

Anonymous said...

The codes helps me a lot. Thanks for unselfishly sharing your knowledge.

Anonymous said...

Thaaaaaaaaaaanks ::):):)

Anonymous said...

this is a good tip. do you have an elegant solution to auto-fit content of datagrid column? meaning, the width will follow the longest value in that specified column. The only solution i know is to loop on all the data and compare. but this one will have a very bad impact for large datasets.

Palmero Tom said...

I'm afraid there's no other solution than the one you proposed...

web development said...

Hi Ria,
This was very helpful tip.
I was trying to implement this for past 3 hours and it was not working.

Thanks a lot
-Sameer

_rootnode said...

i am trying to create a Gantt[timeline] chart in flex using an advanced datagrid. the data grid will have data for an hour by default. i construct the header and the data dynamically. the first column[Name of the event] is fixed and i have a item renderer which spans across all the rest of the columns. my questions is,
1) the data grid takes about 12-15 seconds to render on screen for a reasonable amount of data. is there a way to reduce the time taken. should i override any methods of the data grid?
2) the user will have an option to drill down into the data, say he can choose a specific time frame within the hour. in that case, the header information will vary/reduce. so the data grid should redraw itself.

how do i accomplish these things without affecting the performance.
i am relatively new to flex. any pointers will be highly appreciated.
thanks.

Palmero Tom said...

I wonder whether you should use the AdvancedDataGrid. Is it for display only? In that case I would suggest dynamically instantiating Canvas components in the right place.

_rootnode said...

@RIA
http://ruckfuleshere.blogspot.com/2011/03/datagrid-issue.html

In this pic, the headers represent the minutes of an hour. on drill down the no of mins may vary and so the data.

in that case, the entire grid is to be redrawn.

Palmero Tom said...

Feel free to email me a small example program to demonstrate the problem. I'll take a look at it but again: I wonder whether using the datagrid is the best solution to solve this problem...

Anonymous said...

Hey... resolved my problem... Thanks.