注:我使用的是Flash Builder 4.6
参考文章:http://blog.csdn.net/ycf330/article/details/3862072
-------------------------------------------------------------------------------------------------------------------------------
flex对DataGrid进行增、删、改、查的大致步骤:
1.定义数据源。
2.DataGrid绑定数据源。
3.对数据源进行增删改查
-------------------------------------------------------------------------------------------------------------
界面截图:
-----------------------------------------------------------------------------------------------------------------------------------------------
定义数据源acUser
//记得导入包:import mx.collections.ArrayCollection;
[Bindable]
public var acUser:ArrayCollection=new ArrayCollection([
{id:"1",account:"Anho",pass:"653487"},
{id:"2",account:"Beha",pass:"328756"},
{id:"3",account:"Canv",pass:"987056"}
]);
--------------------------------------------------------------------------------------------------------------------------------------------------------------------
DataGrid绑定数据源acUser:
<s:DataGrid id="dgTest" x="609" y="186" width="333" dataProvider="{acUser}" requestedRowCount="4" selectionChange="dgTest_selectionChangeHandler(event)"> <s:columns> <s:ArrayList> <s:GridColumn dataField="id" headerText="编号"></s:GridColumn> <s:GridColumn dataField="account"headerText="账号"></s:GridColumn> <s:GridColumn dataField="pass" headerText="密码"></s:GridColumn> </s:ArrayList> </s:columns> </s:DataGrid>
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------
对数据源进行增删改查:
//添加 protected function button1_clickHandler(event:MouseEvent):void { acUser.addItem({id:txtId.text,account:txtAccount.text,pass:txtPass.text}); } //删掉所选 protected function button2_clickHandler(event:MouseEvent):void { var idx:int=acUser.getItemIndex(dgTest.selectedItem); acUser.removeItemAt(idx); } //修改所选 protected function button3_clickHandler(event:MouseEvent):void { var item:Object=dgTest.selectedItem; item.id=txtId.text; item.account=txtAccount.text; item.pass=txtPass.text; acUser.itemUpdated(item); } //查找 protected function button4_clickHandler(event:MouseEvent):void { //根据id来查找 var id:int=int(txtId.text); for(var i:int=0;i<acUser.length;i++) { if(acUser.getItemAt(i).id==id) { txtId.text=acUser.getItemAt(i).id txtAccount.text=acUser.getItemAt(i).account; txtPass.text=acUser.getItemAt(i).pass; } } } //项选择发生改变时 protected function dgTest_selectionChangeHandler(event:GridSelectionEvent):void { txtId.text=dgTest.selectedItem.id; txtAccount.text=dgTest.selectedItem.account; txtPass.text=dgTest.selectedItem.pass; }---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
您的十分满意是我追求的宗旨。
您的一点建议是我后续的动力。