DevExpress WPF中文教程:Grid - 如何将更改发布到数据库(设计时)?
DevExpress WPF拥有120个控件和库将帮助您交付满足甚至超出企业需求的高性能业务应用程序。通过DevExpress WPF能创建有着强大互动功能的XAML基础应用程序这些应用程序专注于当代客户的需求和构建未来新一代支持触摸的解决方案。 无论是Office办公软件的衍伸产品还是以数据为中心的商业智能产品都能通过DevExpress WPF控件来实现。本教程演示如何在DevExpress GridControl中完成编辑数据并将更改保存到数据库中。注意本文是基于上文的基础上演变的DevExpress新旧版本帮助文档下载可进QQ qun获取169725316当您启用CRUD(创建、读取、更新、删除)选项时Items Source Wizard项目源向导将添加发布数据功能。Items Source Wizard项目源向导生成以下代码1. 设置TableView.ShowUpdateRowButtons属性为OnCellEditorOpen此属性开启编辑模式允许用户编辑整行然后立即提交或取消所有更改2. 设置TableView.NewItemRowPosition属性为TopNew Item Row新项目行允许用户向GridControl添加新行3. 创建以下命令这些命令是在运行时从带有Command属性的方法生成的生成的命令名遵循[MethodName]Command模式。ValidateRow命令添加新行并将更改保存到数据库中MainViewModel.cs[Command] public void ValidateRow(RowValidationArgs args) { var item (Order)args.Item; if (args.IsNewItem) _Context.Orders.Add(item); _Context.SaveChanges(); }MainViewModel.vbCommand Public Sub ValidateRow(ByVal args As RowValidationArgs) Dim item CType(args.Item, Order) If args.IsNewItem Then _Context.Orders.Add(item) _Context.SaveChanges() End SubValidateRowDeletion命令从数据库中删除项目MainViewModel.cs[Command] public void ValidateRowDeletion(ValidateRowDeletionArgs args) { var item (Order)args.Items.Single(); _Context.Orders.Remove(item); _Context.SaveChanges(); }MainViewModel.vbCommand Public Sub ValidateRowDeletion(ByVal args As ValidateRowDeletionArgs) Dim item CType(args.Items.Single(), Order) _Context.Orders.Remove(item) _Context.SaveChanges() End SubDataSourceRefresh命令从数据库中获取更改并更新网格内容MainViewModel.cs[Command] public void DataSourceRefresh(DataSourceRefreshArgs args) { _ItemsSource null; _Context null; RaisePropertyChanged(nameof(ItemsSource)); }MainViewModel.vbCommand Public Sub DataSourceRefresh(ByVal args As DataSourceRefreshArgs) _ItemsSource Nothing _Context Nothing RaisePropertyChanged(NameOf(ItemsSource)) End SubTableView属性绑定到生成的命令MainView.xamldxg:GridControl x:Namegrid ItemsSource{Binding Orders} !-- ... -- dxg:GridControl.View dxg:TableView NewItemRowPositionTop ShowUpdateRowButtonsOnCellEditorOpen ValidateRowCommand{Binding ValidateRowCommand} ValidateRowDeletionCommand{Binding ValidateRowDeletionCommand} DataSourceRefreshCommand{Binding DataSourceRefreshCommand}/ /dxg:GridControl.View /dxg:GridControlDelete键从GridControl中删除选定的行MainView.xamldxg:GridControl.InputBindings KeyBinding Command{Binding View.Commands.DeleteFocusedRow, ElementNamegrid} KeyDelete/ /dxg:GridControl.InputBindings