翻譯|使用教程|編輯:龔雪|2024-06-19 11:13:28.077|閱讀 116 次
概述:本文將演示如何在DevExpress GridControl中完成編輯數(shù)據(jù)并將更改保存到數(shù)據(jù)庫中,歡迎下載最新版組件體驗(yàn)!
# 界面/圖表報(bào)表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關(guān)鏈接:
DevExpress WPF擁有120+個(gè)控件和庫,將幫助您交付滿足甚至超出企業(yè)需求的高性能業(yè)務(wù)應(yīng)用程序。通過DevExpress WPF能創(chuàng)建有著強(qiáng)大互動(dòng)功能的XAML基礎(chǔ)應(yīng)用程序,這些應(yīng)用程序?qū)W⒂诋?dāng)代客戶的需求和構(gòu)建未來新一代支持觸摸的解決方案。 無論是Office辦公軟件的衍伸產(chǎn)品,還是以數(shù)據(jù)為中心的商業(yè)智能產(chǎn)品,都能通過DevExpress WPF控件來實(shí)現(xiàn)。
本教程演示如何在DevExpress GridControl中完成編輯數(shù)據(jù)并將更改保存到數(shù)據(jù)庫中。(注意本文是基于上文的基礎(chǔ)上演變的,)
DevExpress技術(shù)交流群10:532598169 歡迎一起進(jìn)群討論
當(dāng)您啟用CRUD(創(chuàng)建、讀取、更新、刪除)選項(xiàng)時(shí),Items Source Wizard(項(xiàng)目源向?qū)В⑻砑影l(fā)布數(shù)據(jù)功能。
Items Source Wizard(項(xiàng)目源向?qū)В┥梢韵麓a:
1. 設(shè)置屬性為OnCellEditorOpen,此屬性開啟編輯模式,允許用戶編輯整行,然后立即提交或取消所有更改:
2. 設(shè)置屬性為Top,New Item Row(新項(xiàng)目行)允許用戶向GridControl添加新行:
3. 創(chuàng)建以下命令,這些命令是在運(yùn)行時(shí)從帶有Command屬性的方法生成的,生成的命令名遵循[MethodName]Command模式。
ValidateRow命令添加新行并將更改保存到數(shù)據(jù)庫中:
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命令從數(shù)據(jù)庫中刪除項(xiàng)目:
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命令從數(shù)據(jù)庫中獲取更改并更新網(wǎng)格內(nèi)容:
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>
本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請務(wù)必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請郵件反饋至chenjj@fc6vip.cn
文章轉(zhuǎn)載自:慧都網(wǎng)