DevExpress WinForms拥有180+组件和UI库,能为Windows Forms平台创建具有影响力的业务解决方案。DevExpress WinForms能完美构建流畅、美观且易于使用的应用程序,无论是Office风格的界面,还是分析处理大批量的业务数据,它都能轻松胜任!
DevExpress的数据感知控件与任意数据访问技术(如 ADO.NET、Entity Framework、XPO 等)兼容,并且能够显示来自任何实现 IList、IBindingList 或 ITypedList 接口数据源的数据。请注意,服务器模式和即时反馈模式会对数据绑定施加一定的限制。
Data Grid(或任何其他DevExpress数据感知控件)按原样显示源数据,如果您需要先显示经过筛选或排序的记录,或者将多个数据源合并为一个数据源,那么请在将数据感知控件绑定到该数据源之前,在数据源层面进行操作。
获取DevExpress WinForms v25.2正式版下载
DevExpress技术交流群12:1028386091 欢迎一起进群讨论
创建一个新的数据源
设置新数据源的最快方法是使用Data Source Configuration Wizard(数据源配置向导)。
重要提示:DevExpress Data Source Wizard(数据源向导)调用标准的Visual Studio数据源配置向导,该向导在.NET项目中不可用,您需要手动创建数据集、绑定源和表格适配器(或者您可以在.NET框架项目中生成它们并将其添加到.NET项目中)。
点击Data Grid的智能标签,或者点击网格底部左上角的图标(请参考下方的图片)。
该向导允许您将控件与以下支持的来源进行绑定:
- ADO.NET数据
- SQL 数据
- Excel 工作簿 和 CSV 文件
- JSON 数据
- Entity Framework
- Entity Framework Core
- LINQ to SQL类
- Windows 通信基础架构(WCF)服务
- Open Data v4 服务
- DevExpress ORM工具 (XPO)
- 运行时创建的数据 (IList, IEnumerable等)
- XML数据
- 没有可用强类型数据集的自定义数据
UnboundSource 组件使您能够混合使用不同的数据源类型,或者向已绑定的数据网格中添加虚拟行。
对于基于代码的数据源,可以使用Data Annotation Attributes来标记数据类的属性,从而预先定制网格(例如,防止为特定数据字段生成列,或者更改其内部编辑器的类型)。
选择现有的数据源
如果您已经准备好数据源,请使用Data Grid智能标签,在“Choose Data Source”编辑器中选择该数据源。
在代码中,将一个有效的数据源赋值给GridControl.DataSource属性。
C#
using System.Data.OleDb; // ... // Create a connection object. OleDbConnection connection = new OleDbConnection( "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=E:\\DBs\\NWIND.MDB"); // Create a data adapter. OleDbDataAdapter adapter = new OleDbDataAdapter("SELECT * FROM Products", connection); // Create and fill a dataset. DataSet sourceDataSet = new DataSet(); adapter.Fill(sourceDataSet); // Specify the data source for the grid control. gridControl1.DataSource = sourceDataSet.Tables[0];VB.NET
Imports System.Data.OleDb ' ... ' Create a connection object. Dim Connection As New OleDbConnection( _ "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=E:\\DBs\\NWIND.MDB") ' Create a data adapter. Dim Adapter As New OleDbDataAdapter("SELECT * FROM Products", Connection) ' Create and fill a dataset. Dim SourceDataSet As New DataSet() Adapter.Fill(SourceDataSet) ' Specify the data source for the grid control. GridControl1.DataSource = SourceDataSet.Tables(0)绑定到BindingList源
下面的示例演示如何将网格控件绑定到具有记录对象的BindingList源,Record 类实现了 INotifyPropertyChanged 接口,以便向网格控件通知某个属性值已发生更改。
注意:WinForms网格控件只能绑定到public属性。
C#
using System; using System.Windows.Forms; using System.ComponentModel; using DevExpress.XtraEditors; using System.Runtime.CompilerServices; using System.ComponentModel.DataAnnotations; namespace DXApplication1 { public partial class Form1 : XtraForm { BindingList<Record> records; public Form1() { InitializeComponent(); records = new BindingList<Record>() { new Record(){ CompanyName = "Hanari Carnes", Price = 19.99 }, new Record(){ CompanyName = "Romero y tomillo", Price = 28.99 }, new Record(){ CompanyName = "Reggiani Caseifici", Price = 14.99 }, new Record(){ CompanyName = "Maison Dewey", Price = 32.99 } }; gridControl1.DataSource = records; textEdit1.DataBindings.Add(new Binding("EditValue", records, "CompanyName")); textEdit1.Properties.ValidateOnEnterKey = true; } } public class Record : INotifyPropertyChanged { private Guid idValue = Guid.NewGuid(); [Display(Order = -1)] public Guid ID { get { return this.idValue; } } string text; [DisplayName("Company")] public string CompanyName { get { return text; } set { if (text != value) { if (string.IsNullOrEmpty(value)) throw new Exception(); text = value; OnPropertyChanged(); } } } double? val; [DataType(DataType.Currency)] public double? Price { get { return val; } set { if (val != value) { val = value; OnPropertyChanged(); } } } public event PropertyChangedEventHandler PropertyChanged; protected void OnPropertyChanged([CallerMemberName] string propertyName = "") { if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); } } }VB.NET
Imports System Imports System.Windows.Forms Imports System.ComponentModel Imports DevExpress.XtraEditors Imports System.Runtime.CompilerServices Imports System.ComponentModel.DataAnnotations Namespace DXApplication1 Partial Public Class Form1 Inherits XtraForm Private records As BindingList(Of Record) Public Sub New() InitializeComponent() records = New BindingList(Of Record)() From { New Record() With { .CompanyName = "Hanari Carnes", .Price = 19.99 }, New Record() With { .CompanyName = "Romero y tomillo", .Price = 28.99 }, New Record() With { .CompanyName = "Reggiani Caseifici", .Price = 14.99 }, New Record() With { .CompanyName = "Maison Dewey", .Price = 32.99 } } gridControl1.DataSource = records textEdit1.DataBindings.Add(New Binding("EditValue", records, "CompanyName")) textEdit1.Properties.ValidateOnEnterKey = True End Sub End Class Public Class Record Implements INotifyPropertyChanged Private idValue As Guid = Guid.NewGuid() <Display(Order := -1)> Public ReadOnly Property ID() As Guid Get Return Me.idValue End Get End Property Private text As String <DisplayName("Company")> Public Property CompanyName() As String Get Return text End Get Set(ByVal value As String) If text <> value Then If String.IsNullOrEmpty(value) Then Throw New Exception() End If text = value OnPropertyChanged() End If End Set End Property Private val? As Double <DataType(DataType.Currency)> Public Property Price() As Double? Get Return val End Get Set(ByVal value? As Double) If Not val.Equals(value) Then val = value OnPropertyChanged() End If End Set End Property Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged Protected Sub OnPropertyChanged(Optional <CallerMemberName> ByVal propertyName As String = "") RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(propertyName)) End Sub End Class End Namespace未完待续,更多内容下期见......