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属性的方法生成的,生成的命令名遵循MethodNameCommand模式。

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>
相关推荐
无风听海15 小时前
ASP.NET Core .NET 10 错误响应体系全景:从 BadRequest 到编译器基础设施
后端·asp.net·.net
多巴胺耐受18 小时前
【WPF】炫酷的科技报警弹窗
科技·c#·wpf
Xin_ye1008621 小时前
C# 零基础到精通教程 - WPF 专题二:数据绑定与 MVVM
开发语言·c#·wpf
Xin_ye1008621 小时前
C# 零基础到精通教程 - WPF 专题一:WPF 入门与 XAML 基础
c#·wpf
qq_4312807021 小时前
生成解决方案将文件生成到根目录或指定文件夹下
wpf
AI行业学习2 天前
.NET Framework 3.5 SP1 完整离线包【2026.5.31】
.net
学以智用2 天前
.NET Core 完整特性速查表(终极版)
后端·.net
cdbqss12 天前
VB2026 动态生成工具栏类 BqGetToolStrip
数据库·oracle·开源·.net·学习方法·教育电商·basic
周杰伦fans2 天前
掌握 MVVM Light:.NET 桌面应用开发的 MVVM 利器,掌握 ObservableObject、RelayCommand 和 Messenger
c#·wpf
宝桥南山2 天前
Microsoft Agent Framework(MAF) - 如何将workflow或者A2A client转换成一个AI Agent
microsoft·ai·微软·aigc·.net·.netcore