• 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、运行如下:

  • 相关阅读:
    LeetCode 算法:无重复字符的最长子串c++
    计算机毕业设计(附源码)python元江特色农产品售卖平台
    Vue中的单向数据绑定和双向数据绑定到底是什么
    系列四、Java8的Lambda表达式
    stm32_标准库_中断_按键点灯|蜂鸣器
    nios烧写到EPCS的问题处理
    合肥中科深谷嵌入式项目实战——基于ARM语音识别的智能家居系统(二)
    Redis主从结构数据同步分析
    Android Glide判断图像资源是否缓存onlyRetrieveFromCache,使用缓存数据,Kotlin
    Mac上Qt安装和配置教程
  • 原文地址:https://blog.csdn.net/loongsking/article/details/134381411