当前位置: 首页 > news >正文

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 生成如下代码:

<dxg:GridControl x:Name="grid" AutoGenerateColumns="AddNew" EnableSmartColumnsGeneration="True" ItemsSource="{Binding ItemsSource}" RestoreStateKeyFieldName="OrderId" RestoreStateOnSourceChange="True"><dxg:GridControl.TotalSummary><dxg:GridSummaryItem Alignment="Right" SummaryType="Count"/></dxg:GridControl.TotalSummary><dxg:GridControl.InputBindings><KeyBinding Command="{Binding View.Commands.DeleteFocusedRow, ElementName=grid}" Key="Delete"/></dxg:GridControl.InputBindings><dxg:GridControl.View><dxg:TableView NewItemRowPosition="Top" ShowUpdateRowButtons="OnCellEditorOpen" ValidateRowCommand="{Binding ValidateRowCommand}" ValidateRowDeletionCommand="{Binding ValidateRowDeletionCommand}" DataSourceRefreshCommand="{Binding DataSourceRefreshCommand}" ShowFixedTotalSummary="True"/></dxg:GridControl.View><dxg:GridColumn FieldName="OrderId" IsSmart="True" ReadOnly="True"/><dxg:GridColumn FieldName="CustomerId" IsSmart="True"/><dxg:GridColumn FieldName="EmployeeId" IsSmart="True"/><dxg:GridColumn FieldName="OrderDate" IsSmart="True"/><dxg:GridColumn FieldName="RequiredDate" IsSmart="True"/><dxg:GridColumn FieldName="ShippedDate" IsSmart="True"/><dxg:GridColumn FieldName="ShipVia" IsSmart="True"/><dxg:GridColumn FieldName="Freight" IsSmart="True"/><dxg:GridColumn FieldName="ShipName" IsSmart="True"/><dxg:GridColumn FieldName="ShipAddress" IsSmart="True"/><dxg:GridColumn FieldName="ShipCity" IsSmart="True"/><dxg:GridColumn FieldName="ShipRegion" IsSmart="True"/><dxg:GridColumn FieldName="ShipPostalCode" IsSmart="True"/><dxg:GridColumn FieldName="ShipCountry" IsSmart="True"/>
</dxg:GridControl>
using DevExpress.Mvvm;
using System;
using WPF_DataGrid_GetStarted.Models;
using DevExpress.Mvvm.DataAnnotations;
using System.Linq;
using System.Collections.Generic;
using DevExpress.Mvvm.Xpf;namespace WPF_DataGrid_GetStarted.ViewModels {public class MainViewModel : ViewModelBase {NorthwindEntities _Context;IList<Order> _ItemsSource;public IList<Order> ItemsSource {get {if (_ItemsSource == null && !DevExpress.Mvvm.ViewModelBase.IsInDesignMode) {_Context = new NorthwindEntities();_ItemsSource = _Context.Orders.ToList();}return _ItemsSource;}}[Command]public void ValidateRow(RowValidationArgs args) {var item = (Order)args.Item;if (args.IsNewItem)_Context.Orders.Add(item);_Context.SaveChanges();}[Command]public void ValidateRowDeletion(ValidateRowDeletionArgs args) {var item = (Order)args.Items.Single();_Context.Orders.Remove(item);_Context.SaveChanges();}[Command]public void DataSourceRefresh(DataSourceRefreshArgs args) {_ItemsSource = null;_Context = null;RaisePropertyChanged(nameof(ItemsSource));}}
}

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

7、运行如下:

http://www.lryc.cn/news/228986.html

相关文章:

  • 2023亚太杯数学建模A题思路解析
  • Spark3.0中的AOE、DPP和Hint增强
  • 算法笔记-第五章-质因子分解
  • 适用于WPF的设计模式
  • C++与多态
  • ios 对话框UIAlertController放 tableview
  • 警告:新版Outlook会向微软发送密码、邮件和其他数据
  • 数据结构C语言--基础实验
  • wireshark抓包并进行Eigrp网络协议分析
  • 力扣刷题-二叉树-二叉树的层序遍历(相关题目总结)
  • fundamental notes in 3D math
  • 【Java 进阶篇】JQuery DOM操作:舞动网页的属性魔法
  • 腾讯云5年云服务器还有吗?腾讯云5年时长服务器入口在哪?
  • odoo在iot领域的发展情况
  • Paas-云原生-容器-编排-持续部署
  • sass 生成辅助色
  • DevEco Studio开发工具下载、安装(HarmonyOS开发)_For Mac
  • 按键精灵中的字符串常用的场景
  • python数据结构与算法-01_抽象数据类型和面向对象编程
  • 纯手写 模态框、消息弹框、呼吸灯
  • windows安装composer并更换国内镜像
  • c++ basic_string 和string区别
  • 怎么在uni-app中使用Vuex 简单demo,通俗易懂(第二篇)
  • 相机专业模型详解,各个参数作用,专业模式英文全称和缩写
  • 将随机数设成3407,让你的深度学习模型再涨一个点!文再附3种随机数设定方法
  • Spring后端HttpClient实现微信小程序登录
  • Linux下部署MySQL-MHA环境
  • DaoWiki(基于Django)开发笔记 20231114-阿里云mysql外部访问
  • 【UE5】 虚拟制片教程
  • 集成Line、Facebook、Twitter、Google、微信、QQ、微博、支付宝的三方登录sdk