<? XML version = "1.0"?>
<Mx: Application xmlns: MX = "http://www.adobe.com/2006/mxml">
<Mx: SCRIPT>
<! [CDATA [
Import MX. utils. stringutil;
Import MX. Controls. textinput;
Import MX. Events. Maid;
Import MX. Events. datagridevent;
Import MX. Controls. numericstepper;
Import MX. Collections. arraycollection;
Import MX. Controls. listclasses. idropinlistitemrenderer;
[Bindable]
Private var mydp: arraycollection = new arraycollection ([
{ARTIST: 'pavement ', album: 'slanted and enchanted', price: 11.99 },
{ARTIST: 'pavement ', album: 'crooked rain, crooked rain', price: 10.99 },
{ARTIST: 'pavement ', album: 'wowee zowee', price: 12.99 },
{ARTIST: 'pavement ', album: 'brighten the corn', price: 11.99 },
{ARTIST: 'pavement ', album: 'terror twilight', price: 11.99}
]);
// Verify the validity of the input data
Private function validateitem (Event: datagridevent): void {
// Return directly if no data is updated
If (event. Reason = maid. cancelled ){
Return;
}
VaR input: textinput = textinput (_ grid. itemeditorinstance );
VaR newdata: String = textinput (event. currenttarget. itemeditorinstance). text;
If (event. datafield = "price "){
VaR pattern: Regexp =/(0 + /. [0-9] * [1-9] {1,}) | ([1-9] [0-9] */. /d) | ([1-9] *) $ /;
Newdata = pattern.exe C (newdata) [0];
If (stringutil. Trim (newdata). Length <= 0 ){
Event. preventdefault ();
Input. errorstring = "The number format is incorrect! ";
Return;
}
}
}
// If the number is input, only numbers and decimal places can be entered in the input box.
Private function doitemeditbegin (Event: datagridevent): void {
VaR colname: String = _ grid. Columns [event. columnindex]. datafield;
If (colname = "price "){
_ Grid. createitemeditor (event. columnindex, event. rowindex );
_ Grid. itemeditorinstance. Data = _ grid. editeditemrenderer. Data;
Textinput (_ grid. itemeditorinstance). Restrict = "0-9 /.";
}
}
]>
</MX: SCRIPT>
<Mx: DataGrid id = "_ grid" dataprovider = "{mydp }"
Editable = "true"
Itemeditbegin = "doitemeditbegin (event );"
Itemeditend = "validateitem (event )"
>
<Mx: columns>
<Mx: datagridcolumn datafield = "artist"/>
<Mx: datagridcolumn datafield = "album" width = "130"/>
<Mx: datagridcolumn datafield = "price"/>
</MX: columns>
</MX: DataGrid>
</MX: Application>