翻譯|使用教程|編輯:龔雪|2024-06-19 11:13:28.077|閱讀 116 次
概述:本文將演示如何在DevExpress GridControl中完成編輯數據并將更改保存到數據庫中,歡迎下載最新版組件體驗!
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關鏈接:
DevExpress WPF擁有120+個控件和庫,將幫助您交付滿足甚至超出企業需求的高性能業務應用程序。通過DevExpress WPF能創建有著強大互動功能的XAML基礎應用程序,這些應用程序專注于當代客戶的需求和構建未來新一代支持觸摸的解決方案。 無論是Office辦公軟件的衍伸產品,還是以數據為中心的商業智能產品,都能通過DevExpress WPF控件來實現。
本教程演示如何在DevExpress GridControl中完成編輯數據并將更改保存到數據庫中。(注意本文是基于上文的基礎上演變的,)
DevExpress技術交流群10:532598169 歡迎一起進群討論
當您啟用CRUD(創建、讀取、更新、刪除)選項時,Items Source Wizard(項目源向導)將添加發布數據功能。
Items Source Wizard(項目源向導)生成以下代碼:
1. 設置屬性為OnCellEditorOpen,此屬性開啟編輯模式,允許用戶編輯整行,然后立即提交或取消所有更改:
2. 設置屬性為Top,New 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.vb
<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
[Command] public void ValidateRowDeletion(ValidateRowDeletionArgs args) { var item = (Order)args.Items.Single(); _Context.Orders.Remove(item); _Context.SaveChanges(); }
MainViewModel.vb
<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
[Command] public void DataSourceRefresh(DataSourceRefreshArgs args) { _ItemsSource = null; _Context = null; RaisePropertyChanged(nameof(ItemsSource)); }
MainViewModel.vb
<Command> Public Sub DataSourceRefresh(ByVal args As DataSourceRefreshArgs) _ItemsSource = Nothing _Context = Nothing RaisePropertyChanged(NameOf(ItemsSource)) End Sub
TableView屬性綁定到生成的命令:
MainView.xaml
<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
<dxg:GridControl.InputBindings> <KeyBinding Command="{Binding View.Commands.DeleteFocusedRow, ElementName=grid}" Key="Delete"/> </dxg:GridControl.InputBindings>
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn
文章轉載自:慧都網