In a previous post I talked about my preferred method in Flex 2 for dealing with XML data that contains namespaces. Another task that gets complicated by the presence of 'namespaced XML' is populating a DataGrid with that data. Despite the usefulness of the use namespace directive, I have been unable to populate a DataGrid just by using the dataField attribute, as can be done when your data does not contain namespaces.

<mx:DataGrid x="10" y="291">
    <mx:columns>
        <mx:DataGridColumn headerText="Column 1" dataField="col1"/>
        <mx:DataGridColumn headerText="Column 2" dataField="col2"/>
    </mx:columns>
</mx:DataGrid>

The method I employ uses the labelFunction attribute of DataGridColumn, something that is most commonly used to let you specify a function to handle the formatting of data you would like displayed in that column. This approach allows you to use a single function for both special formatting of data and to gain access to data that resides in a namespace. Here is the basic format of my labelFunction:

private function genericLabelFunction(obj_data:Object, obj_dataGridColumn:DataGridColumn):String
{
    use namespace DOCUMENT_METADATA_NAMESPACE;
    var q:QName = new QName(DOCUMENT_METADATA_NAMESPACE, obj_dataGridColumn.dataField);
    var str_data:String = obj_data[q];

    switch(obj_dataGridColumn.dataField)
    {
        case "Status":
            return (str_data == "InProgress") ? "In Progress" : str_data;
            break;
        case "PlanDataDate":
            return str_data.substr(0, str_data.indexOf(" "))
            break;
        default:
            return str_data;
            break;
    }
}

Another item that has proven useful for dealing with namespaced XML is the QName class. Short for 'qualified name', the QName constructor allows you to specify a namespace and a local name, which it uses to address the data you are targeting. Once we establish our QName object, we use it to reference the desired property of the data object (obj_data) that is passed into our labelFunction. Also notice that the local name is determined by the dataField attribute of the DataGridColumn object that was passed into the function. We now have a variable (str_data) that should contain the data from the node in our XML that has the same name as specified by our dataField attribute.

As shown in the first two cases, you can do further formatting to this data if desired. The default case takes care of all non-special cases, for when you would like to display the data as it exists in the XML and were simply using the labelFunction to get around namespace issues. Finally, here is a simple example of a DataGrid that uses our genericLabelFunction.

<mx:DataGrid dataProvider="{ModelLocator.getInstance().clientPlanList}" id="planList_dg">
    <mx:columns>
        <mx:DataGridColumn headerText="Industry" dataField="Industry" labelFunction="genericLabelFunction"/>
        <mx:DataGridColumn headerText="Plan Date" dataField="PlanDataDate" labelFunction="genericLabelFunction"/>
        <mx:DataGridColumn headerText="Status" dataField="Status" labelFunction="genericLabelFunction"/>
        <mx:DataGridColumn headerText="Group Size" dataField="PlanSizeGroup" labelFunction="genericLabelFunction"/>
    </mx:columns>
</mx:DataGrid>