views:

33

answers:

2

I want to add a ComboBox to a DataGrid. So far, the only way I've found to do it is like this:

<mx:DataGridColumn headerText="Header" dataField="src" >
<mx:itemRenderer>
<mx:Component>
<mx:ComboBox dataProvider="{data.srcChoices}" />
</mx:Component>                                             
</mx:itemRenderer>
</mx:DataGridColumn>

The problem is the initial value of the ComboBox isn't set correctly. If I hard code the choices, then the initial value is set correctly. I can't hard code the choices. Any idea what I should do?

+1  A: 

1) You can add content to the combobox and set the "selectedItem" value to whatever the proper index is.

<mx:ComboBox dataProvider="{data.srcChoices}" selectedindex="2" />

2) You can give your combobox an ID and then select the item with ID.selectedIndex in a script

ID.selectedIndex = 2;
+1  A: 

You probably want the initial value to be set based on a variable from the value object, and not a magic number. To do this, you have to do something like this:

selectedIndex="{(outerDocument as WorkHist).findItemIndexOccupation(data.Occupation)}" dataProvider="{outerDocument.occData}"

where WorkHist is the Canvas, or whatever component the DG is living in.

public function findItemIndexOccupation(data:String):int {
    for (var i:int = 0; i < occData.length; i++) {
        if (occData.getItemAt(i).POSITION == data) {
            return i;
        }
    }
    return -1;
}
houser2112
Thank you, works perfectly.
FigBug