DevExpress WPF中文教程:Grid - 如何向项目添加GridControl并绑定到数据 ...

打印 上一主题 下一主题

主题 583|帖子 583|积分 1749

DevExpress WPF拥有120+个控件和库,将帮助您交付满足以致超出企业需求的高性能业务应用程序。通过DevExpress WPF能创建有着强盛互动功能的XAML基础应用程序,这些应用程序专注于今世客户的需求和构建未来新一代支持触摸的解决方案。 无论是Office办公软件的衍伸产物,还是以数据为中心的贸易智能产物,都能通过DevExpress WPF控件来实现。
本教程演示了如何将GridControl添加到项目中,并将控件绑定到数据库:


获取DevExpress WPF v23.2正式版下载(Q技术交换:532598169)
1. 利用DevExpress Template Gallery(模板库)来创建一个新的Blank MVVM Application(空缺MVVM应用程序),这个项目模版包罗了一个样板View Model类,并设置MainView的数据上下文:


2. 将项目连接到本地数据库,示比方下:Blank .NET 6 App with the Northwind Database 。
3. 将GridControl工具箱项添加到MainView:


假如您的项目没有DevExpress.Wpf.Grid.Core.v23.2引用,Visual Studio将显示以下消息:


此消息关照您已添加所需的引用,并要求再次添加控件。
提示:假如您从NuGet源而不是从同一组件安装程序中获取DevExpress产物,则工具箱中不包罗DevExpress控件,除非您添加相应的NuGet包。
跳转到Tools | NuGet Package Manager | Manage NuGet Packages for Solution,然后添加DevExpress.Wpf.Grid NuGet包。
4. 选择GridControl然后调用它的Quick Actions(快捷操纵)菜单,单击Bind to a Data Source来启动Items Source Wizard(项目源向导):


5. 选择一个数据源:


选择要在GridControl中显示的表:


选择Simple Binding模子。


确保启用了CRUD选项:


选择View Model选项,在View Model中生成数据绑定代码。确认MainViewModel类被选择为View Model:


6. Items Source Wizard(项目源向导)生成以下代码:
MainView.xaml
  1. <dxg:GridControl x:Name="grid" AutoGenerateColumns="AddNew" EnableSmartColumnsGeneration="True" ItemsSource="{Binding ItemsSource}" RestoreStateKeyFieldName="OrderId" RestoreStateOnSourceChange="True">
  2. <dxg:GridControl.TotalSummary>
  3. <dxg:GridSummaryItem Alignment="Right" SummaryType="Count"/>
  4. </dxg:GridControl.TotalSummary>
  5. <dxg:GridControl.InputBindings>
  6. <KeyBinding Command="{Binding View.Commands.DeleteFocusedRow, ElementName=grid}" Key="Delete"/>
  7. </dxg:GridControl.InputBindings>
  8. <dxg:GridControl.View>
  9. <dxg:TableView NewItemRowPosition="Top" ShowUpdateRowButtons="OnCellEditorOpen" ValidateRowCommand="{Binding ValidateRowCommand}" ValidateRowDeletionCommand="{Binding ValidateRowDeletionCommand}" DataSourceRefreshCommand="{Binding DataSourceRefreshCommand}" ShowFixedTotalSummary="True"/>
  10. </dxg:GridControl.View>
  11. <dxg:GridColumn FieldName="OrderId" IsSmart="True" ReadOnly="True"/>
  12. <dxg:GridColumn FieldName="CustomerId" IsSmart="True"/>
  13. <dxg:GridColumn FieldName="EmployeeId" IsSmart="True"/>
  14. <dxg:GridColumn FieldName="OrderDate" IsSmart="True"/>
  15. <dxg:GridColumn FieldName="RequiredDate" IsSmart="True"/>
  16. <dxg:GridColumn FieldName="ShippedDate" IsSmart="True"/>
  17. <dxg:GridColumn FieldName="ShipVia" IsSmart="True"/>
  18. <dxg:GridColumn FieldName="Freight" IsSmart="True"/>
  19. <dxg:GridColumn FieldName="ShipName" IsSmart="True"/>
  20. <dxg:GridColumn FieldName="ShipAddress" IsSmart="True"/>
  21. <dxg:GridColumn FieldName="ShipCity" IsSmart="True"/>
  22. <dxg:GridColumn FieldName="ShipRegion" IsSmart="True"/>
  23. <dxg:GridColumn FieldName="ShipPostalCode" IsSmart="True"/>
  24. <dxg:GridColumn FieldName="ShipCountry" IsSmart="True"/>
  25. </dxg:GridControl>
复制代码
MainViewModel.cs
  1. using DevExpress.Mvvm;
  2. using System;
  3. using WPF_DataGrid_GetStarted.Models;
  4. using DevExpress.Mvvm.DataAnnotations;
  5. using System.Linq;
  6. using System.Collections.Generic;
  7. using DevExpress.Mvvm.Xpf;
  8. namespace WPF_DataGrid_GetStarted.ViewModels {
  9. public class MainViewModel : ViewModelBase {
  10. NorthwindEntities _Context;
  11. IList<Order> _ItemsSource;
  12. public IList<Order> ItemsSource {
  13. get {
  14. if (_ItemsSource == null && !DevExpress.Mvvm.ViewModelBase.IsInDesignMode) {
  15. _Context = new NorthwindEntities();
  16. _ItemsSource = _Context.Orders.ToList();
  17. }
  18. return _ItemsSource;
  19. }
  20. }
  21. [Command]
  22. public void ValidateRow(RowValidationArgs args) {
  23. var item = (Order)args.Item;
  24. if (args.IsNewItem)
  25. _Context.Orders.Add(item);
  26. _Context.SaveChanges();
  27. }
  28. [Command]
  29. public void ValidateRowDeletion(ValidateRowDeletionArgs args) {
  30. var item = (Order)args.Items.Single();
  31. _Context.Orders.Remove(item);
  32. _Context.SaveChanges();
  33. }
  34. [Command]
  35. public void DataSourceRefresh(DataSourceRefreshArgs args) {
  36. _ItemsSource = null;
  37. _Context = null;
  38. RaisePropertyChanged(nameof(ItemsSource));
  39. }
  40. }
  41. }
复制代码
MainViewModel.vb
  1. Imports DevExpress.Mvvm
  2. Imports System
  3. Imports WPF_DataGrid_GetStarted.Models
  4. Imports DevExpress.Mvvm.DataAnnotations
  5. Imports System.Linq
  6. Imports System.Collections.Generic
  7. Imports DevExpress.Mvvm.Xpf
  8. Namespace WPF_DataGrid_GetStarted.ViewModels
  9. Public Class MainViewModel
  10. Inherits ViewModelBase
  11. Private _Context As NorthwindEntities
  12. Private _ItemsSource As IList(Of Order)
  13. Public ReadOnly Property ItemsSource As IList(Of Order)
  14. Get
  15. If _ItemsSource Is Nothing AndAlso Not DevExpress.Mvvm.ViewModelBase.IsInDesignMode Then
  16. _Context = New NorthwindEntities()
  17. _ItemsSource = _Context.Orders.ToList()
  18. End If
  19. Return _ItemsSource
  20. End Get
  21. End Property
  22. <Command>
  23. Public Sub ValidateRow(ByVal args As RowValidationArgs)
  24. Dim item = CType(args.Item, Order)
  25. If args.IsNewItem Then _Context.Orders.Add(item)
  26. _Context.SaveChanges()
  27. End Sub
  28. <Command>
  29. Public Sub ValidateRowDeletion(ByVal args As ValidateRowDeletionArgs)
  30. Dim item = CType(args.Items.Single(), Order)
  31. _Context.Orders.Remove(item)
  32. _Context.SaveChanges()
  33. End Sub
  34. <Command>
  35. Public Sub DataSourceRefresh(ByVal args As DataSourceRefreshArgs)
  36. _ItemsSource = Nothing
  37. _Context = Nothing
  38. RaisePropertyChanged(NameOf(ItemsSource))
  39. End Sub
  40. End Class
  41. End Namespace
复制代码
这段代码启用CRUD操纵,为全部数据源字段生成列,并在固定的汇总面板中显示总行数。
7. 运行项目:



更多DevExpress线上公开课、中文教程资讯请上中文网获取

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

北冰洋以北

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表