Setting explicit columns on a Spark DataGrid control in Flex Hero

The following example shows how you can set explicit columns on a Spark DataGrid control in Flex Hero by setting the columns property to an IList implementation (for example ArrayCollection or ArrayList).

The following example(s) require Flash Player 10 and the beta Adobe Flex Hero SDK. To download the Adobe Flash Builder 4 trial, see www.adobe.com/products/flex/. To download the latest nightly build of the Flex Hero SDK, see opensource.adobe.com/wiki/display/flexsdk/Download+Flex+Hero.

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2010/10/24/setting-explicit-columns-on-a-spark-datagrid-control-in-flex-hero/ -->
<s:Application name="Spark_DataGrid_columns_test"
        xmlns:fx="http://ns.adobe.com/mxml/2009" 
        xmlns:s="library://ns.adobe.com/flex/spark" 
        xmlns:mx="library://ns.adobe.com/flex/mx">
 
    <s:DataGrid id="dg" horizontalCenter="0" verticalCenter="0">
        <s:columns>
            <s:ArrayList>
                <s:GridColumn dataField="fontName" />
            </s:ArrayList>
        </s:columns>
        <s:dataProvider>
            <s:ArrayList source="{Font.enumerateFonts(true).sortOn('fontName')}" />
        </s:dataProvider>
    </s:DataGrid>
 
</s:Application>

This entry is based on a beta version of the Flex Hero SDK and therefore is very likely to change as development of the Flex SDK continues. The API can (and will) change causing examples to possibly not compile in newer versions of the Flex Hero SDK.

2 thoughts on “Setting explicit columns on a Spark DataGrid control in Flex Hero

  1. Hi

    I’ve been testing the Spark datagrid for a while and i cant figure out one thing:
    How can i get the x position of the columns?
    I’ve tried to do it this way: dataGrid.columns.getItemAt(2).width
    This gives back NaN, unless i specify the size of the column, or resize the column.

    In the debugger, i saw that these properties are in
    dataGrid.grid.gridLayout.gridDimensions._columnwidths – but this is a private property.
    Anyone know a way to get these values?
    Thanks

  2. Flashbuilder 4.5.1:
    Using an arrayList as dataProvider will cause that you cannot use the sortableColumns functionality of a spark datagrid. With an arrayCollection as dataProvider sortableColumns is working.

Comments are closed.