Follow Flex After Dark on Twitter

Flex DataGrid Control (mx.controls.DataGrid)

The DataGrid control (mx.controls.DataGrid) displays tabular data (rows and columns) in a Flex application.

DataGrid TODO: embed example (DataGrid1)

The code below gives an example of creating a simple, empty DataGrid with three columns.

   <mx:DataGrid>
      <mx:columns>
         <mx:DataGridColumn headerText="First Name" />
         <mx:DataGridColumn headerText="Last Name" />
         <mx:DataGridColumn headerText="Email Address" />
      </mx:columns>
   </mx:DataGrid>

Populating a DataGrid

To populate a DataGrid we'll have to provide it with data via the dataProvider property, and we'll have to tell each column which field in the provided data to use via it's dataField property.

   <mx:DataGrid dataProvider="{this.contacts}">
      <mx:columns>
         <mx:DataGridColumn headerText="First Name" dataField="firstName" />
         <mx:DataGridColumn headerText="Last Name" dataField="lastName" />
         <mx:DataGridColumn headerText="Email Address" dataField="emailAddress" />
      </mx:columns>
   </mx:DataGrid>
   
   <mx:Array id="contacts">
      <mx:Object firstName="Joe" lastName="Smith" emailAddress="joe@smith.com" />
      <mx:Object firstName="Sally" lastName="Lally" emailAddress="sally@lally.com" />
      <mx:Object firstName="Albert" lastName="Rigdon" emailAddress="albert@rigdon.com" />
   </mx:Array>

The above code does three things:

  • Creates an array of objects (with id=contacts)
  • Assigns the array as the <mx:DataGrid>'s dataProvider property
  • Specifies each <mx:DataGridColumn>'s dataField property

TODO... embed example (X_DataGrid1)

Of course data in real world applications isn't going to be static as in the examples. For rich, dynamic data will make use of Data Providers.

Populating a DataGrid with XML

The below example is very similar to the one above except XML is used as the dataProvider. Note that each <mx:DataGridColumn>'s dataField property is prefixed with @ for referencing an XML attribute.

   <mx:DataGrid dataProvider="{this.contactsXML.contact}">
      <mx:columns>
         <mx:DataGridColumn headerText="First Name" dataField="@firstName" />
         <mx:DataGridColumn headerText="Last Name" dataField="@lastName" />
         <mx:DataGridColumn headerText="Email Address" dataField="@emailAddress" />
      </mx:columns>
   </mx:DataGrid>
   
   <mx:XML id="contactsXML">
      <contacts>
         <contact firstName="Joe" lastName="Smith" emailAddress="joe@smith.com" />
         <contact firstName="Sally" lastName="Lally" emailAddress="sally@lally.com" />
         <contact firstName="Albert" lastName="Rigdon" emailAddress="albert@rigdon.com" />
      </contacts>
   </mx:XML>

Handling DataGrid Events

The above example also shows how a DataGrid can dispatch events, for instance when a row is clicked on.

Advanced DataGrid Features

The DataGrid control provides the following features:

  • Columns of different widths or identical fixed widths
  • Columns that the user can resize at runtime
  • Columns that the user can reorder at runtime
  • Optional customizable column headers
  • Ability to use a custom item renderer for any column to display data other than text
  • Support for sorting the data by clicking on a column

Column Sorting

TODO...

Learn more about Collection Sorting...

Related Links:

Related Docs
  • Flex Item Renderers A Flex Item Renderer is a component used by data-driven controls, including lists, for displaying each item of a data provider.
  • Flex List Controls Flex List controls refer to a special group of controls that display collections of items, usually from a data provider.
  • Flex Data Providers Several Flex components, including all list-based controls, display data from a data provider, a collection or object that contains the data displayed by the control.
Recent Docs
  • Flex DataGrid The Flex DataGrid control is used for displaying tabular data.
  • ActionScript E4X E4X (ECMAScript for XML) enables ActionScript to handle XML as a first-class citizen.
  • Flex Embedding Assets Overview of embedding assets in Flex applications.
  • Flex List Controls Flex List controls refer to a special group of controls that display collections of items, usually from a data provider.
  • ActionScript ArrayCollection The ArrayCollection class is a robust collection class that can be more useful than a standard array.
Random Docs