• wpf devexpress项目中添加GridControl绑定数据


    本教程讲解了如何添加GridControl到wpf项目中并且绑定数据

    原文地址Lesson 1 - Add a GridControl to a Project and Bind it to Data | WPF Controls | DevExpress Documentation

    1、使用 DevExpress Template Gallery创建一个新的空白mvvm应用程序,这个项目包括了一个视图模型,设置此视图模型作为MainView数据上下文

    2、按照如下给此项目添加数据库Blank .NET 6 App with the Northwind Database .

    3、在MainView中添加工具箱选项GridControl:

    如果你的项目没有DevExpress.Wpf.Grid.Core 引用,vs一定显示如下信息:

    此消息提示你必须添加控件引用。

    如果你从Nuget订阅devexpress,进入工具,nuget包管理器,添加DevExpress.Wpf.Grid

    4、在Quick Actions菜单中选择GridControl,点击绑定到数据源,在Items Source Wizard:

    5、选择数据源:

    在GridControl选择table:

    选择简单绑定模型,查看如下提示消息关于绑定模型:WPF Data Grid: Bind to Data.

    确保CRUD 选项启动:

    在视图模型选择视图模型选项生成数据绑定源码。在已选择的View Model中选择MainViewModel:

    6、Items Source Wizard 生成如下代码:

    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>
    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 _ItemsSource;
    12. public IList 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. }

    这个代码启动CRUD operations ,为每一个数据源字段生成行,然后显示所有行数在fixed summary panel.

    7、运行如下:

  • 相关阅读:
    MySQL 新增表中的数据为另外一个或多个表的数据(业务场景:创建关系表,复制旧表数据到新表)
    弧形进度条,弧形百分比
    Enhance Stock Analysis with the SMI Indicator
    【2023】Redis数据持久化
    纳米尺度下的单粒子追踪,厦门大学方宁团队用 AI 奏响「细胞里的摇滚」
    关于#网络#的问题:采用CRC方法进行校验,求数据冗余码FCS(相关搜索:计算机网络)
    c++封装webrtc sdk(一):设计sdk基本结构
    复杂AB实验
    外贸邮箱推荐,提升国际业务沟通效率的5大邮箱
    “torch.load“中出现的“Unexpected key(s) in state_dict“报错问题
  • 原文地址:https://blog.csdn.net/loongsking/article/details/134381411