DevExpress WPF中文教程:Grid - 如何将更改发布到数据库(设计时)?

DevExpress WPF拥有120+个控件和库,将帮助您交付满足甚至超出企业需求的高性能业务应用程序。通过DevExpress WPF能创建有着强大互动功能的XAML基础应用程序,这些应用程序专注于当代客户的需求和构建未来新一代支持触摸的解决方案。 无论是Office办公软件的衍伸产品,还是以数据为中心的商业智能产品,都能通过DevExpress WPF控件来实现。

本教程演示如何在DevExpress GridControl中完成编辑数据并将更改保存到数据库中。(注意本文是基于上文的基础上演变的,点击这里可回顾>>

获取DevExpress WPF v24.1正式版下载(Q技术交流:532598169)

当您启用CRUD(创建、读取、更新、删除)选项时,Items Source Wizard(项目源向导)将添加发布数据功能。

Items Source Wizard(项目源向导)生成以下代码:

  1. 设置TableView.ShowUpdateRowButtons属性为OnCellEditorOpen,此属性开启编辑模式,允许用户编辑整行,然后立即提交或取消所有更改:
  1. 设置TableView.NewItemRowPosition属性为Top,New Item Row(新项目行)允许用户向GridControl添加新行:
  1. 创建以下命令,这些命令是在运行时从带有Command属性的方法生成的,生成的命令名遵循[MethodName]Command模式。

ValidateRow命令添加新行并将更改保存到数据库中:

MainViewModel.cs

cs 复制代码
[Command]
public void ValidateRow(RowValidationArgs args) {
var item = (Order)args.Item;
if (args.IsNewItem)
_Context.Orders.Add(item);
_Context.SaveChanges();
}

MainViewModel.vb

vbnet 复制代码
<Command>
Public Sub ValidateRow(ByVal args As RowValidationArgs)
Dim item = CType(args.Item, Order)
If args.IsNewItem Then _Context.Orders.Add(item)
_Context.SaveChanges()
End Sub

ValidateRowDeletion命令从数据库中删除项目:

MainViewModel.cs

cs 复制代码
[Command]
public void ValidateRowDeletion(ValidateRowDeletionArgs args) {
var item = (Order)args.Items.Single();
_Context.Orders.Remove(item);
_Context.SaveChanges();
}

MainViewModel.vb

vbnet 复制代码
<Command>
Public Sub ValidateRowDeletion(ByVal args As ValidateRowDeletionArgs)
Dim item = CType(args.Items.Single(), Order)
_Context.Orders.Remove(item)
_Context.SaveChanges()
End Sub

DataSourceRefresh命令从数据库中获取更改并更新网格内容:

MainViewModel.cs

cs 复制代码
[Command]
public void DataSourceRefresh(DataSourceRefreshArgs args) {
_ItemsSource = null;
_Context = null;
RaisePropertyChanged(nameof(ItemsSource));
}

MainViewModel.vb

vbnet 复制代码
<Command>
Public Sub DataSourceRefresh(ByVal args As DataSourceRefreshArgs)
_ItemsSource = Nothing
_Context = Nothing
RaisePropertyChanged(NameOf(ItemsSource))
End Sub

TableView属性绑定到生成的命令:

MainView.xaml

XML 复制代码
<dxg:GridControl x:Name="grid" ItemsSource="{Binding Orders}">
<!-- ... -->
<dxg:GridControl.View>
<dxg:TableView NewItemRowPosition="Top"
ShowUpdateRowButtons="OnCellEditorOpen"
ValidateRowCommand="{Binding ValidateRowCommand}"
ValidateRowDeletionCommand="{Binding ValidateRowDeletionCommand}"
DataSourceRefreshCommand="{Binding DataSourceRefreshCommand}"/>
</dxg:GridControl.View>
</dxg:GridControl>

Delete键从GridControl中删除选定的行:

MainView.xaml

XML 复制代码
<dxg:GridControl.InputBindings>
<KeyBinding Command="{Binding View.Commands.DeleteFocusedRow, ElementName=grid}" Key="Delete"/>
</dxg:GridControl.InputBindings>
相关推荐
heimeiyingwang7 小时前
【架构实战】状态机架构:订单/工单状态流转设计
观察者模式·架构·wpf
rockey62712 小时前
AScript之eval函数详解
c#·.net·script·eval·expression·动态脚本
周杰伦fans1 天前
AutoCAD .NET 二次开发:深入理解 EntityJig 的工作原理与正确实现
开发语言·.net
KmSH8umpK2 天前
Redis分布式锁从原生手写到Redisson高阶落地,附线上死锁复盘优化方案进阶第三篇
redis·分布式·wpf
KmSH8umpK2 天前
Redis分布式锁从原生手写到Redisson高阶落地,附线上死锁复盘优化方案
redis·分布式·wpf
William_cl2 天前
【C#/.NET 进阶】ASP.NET 架构与最佳实践:DI 依赖注入(IoC 核心)从入门到避坑
c#·asp.net·.net
武藤一雄2 天前
WPF:MessageBox系统消息框
前端·microsoft·c#·.net·wpf
武藤一雄2 天前
WPF进阶:万字详解WPF如何性能优化
windows·性能优化·c#·.net·wpf·.netcore·鲁棒性
wangnaisheng2 天前
【WPF】路由事件详细使用
wpf
雨浓YN3 天前
GKMLT通讯工具箱(WPF MVVM) - 07-倍福ADS通讯
网络·wpf