Problem
I'd like to have a Delete button in a DataGrid column, and when that button is clicked, that row of the DataGrid is removed.
Solution
In the Delete button click handler, access the DataGrid dataProvider and use the selectedIndex property to delete the item.
Detailed explanation
I'd like to have a Delete button in a DataGrid column, and when that button is clicked, that row of the DataGrid is removed.
In the Delete button click handler, access the DataGrid dataProvider and use the selectedIndex property to delete the item.


<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml">
<mx:Script>
<![CDATA[
import mx.controls.DataGrid;
import mx.collections.XMLListCollection;
private var xml:XML =
<root>
<item>string one</item>
<item>string two</item>
<item>string three</item>
<item>string four</item>
<item>string five</item>
<item>string six</item>
</root>;
[Bindable] private var xc:XMLListCollection = new
XMLListCollection(xml..item);
public function deleteItem(event:MouseEvent):void{
xc.removeItemAt(dg.selectedIndex);
xc.refresh();
}
]]>
</mx:Script>
<mx:DataGrid id="dg" dataProvider="{xc}">
<mx:columns>
<mx:DataGridColumn headerText="String Text"
dataField="item">
<mx:itemRenderer>
<mx:Component>
<mx:Text text="{data}"/>
</mx:Component>
</mx:itemRenderer>
</mx:DataGridColumn>
<mx:DataGridColumn headerText="Delete Item">
<mx:itemRenderer>
<mx:Component>
<mx:LinkButton label="Delete"
click="outerDocument.deleteItem(event)"/>
</mx:Component>
</mx:itemRenderer>
</mx:DataGridColumn>
</mx:columns>
</mx:DataGrid>
</mx:Application>
note:
outerDocument keyword
we can use outerDocument keyword to acess the variable define outside the componet in an mxml