界面控件DevExpress WinForms中文教程:Data Grid - 数据绑定(二)
DevExpress WinForms拥有180组件和UI库能为Windows Forms平台创建具有影响力的业务解决方案。DevExpress WinForms能完美构建流畅、美观且易于使用的应用程序无论是Office风格的界面还是分析处理大批量的业务数据它都能轻松胜任DevExpress的数据感知控件与任意数据访问技术如 ADO.NET、Entity Framework、XPO 等兼容并且能够显示来自任何实现 IList、IBindingList 或 ITypedList 接口数据源的数据。请注意服务器模式和即时反馈模式会对数据绑定施加一定的限制。Data Grid或任何其他DevExpress数据感知控件按原样显示源数据如果您需要先显示经过筛选或排序的记录或者将多个数据源合并为一个数据源那么请在将数据感知控件绑定到该数据源之前在数据源层面进行操作。在上文中点击这里回顾我们介绍了如何创建一个新的数据源、选择现有的数据源、绑定到BindingList源等本文将继续介绍如何切换另一个数据源等欢迎持续关注我们~获取DevExpress WinForms v25.2正式版下载切换到另一个数据源一旦您首次选择了数据源Data Grid会自动生成所有必需的列。如果之后您选择了其他数据源之前创建的列仍会保留这些列需要手动进行更新。要进行更新请启动Data Grid Designer数据网格设计器切换到 “Columns”选项卡然后点击“Retrieve Fields”按钮 。如果您需要在代码中更改数据源请将GridControl.DataSource属性重置为 null在 Visual Basic 中为 Nothing然后为该属性赋值一个新的数据源对象建议将此代码包含在BeginUpdate()和 EndUpdate()方法调用之中。C#gridControl1.BeginUpdate(); try { gridView1.Columns.Clear(); gridControl1.DataSource null; gridControl1.DataSource newDataSource ; } finally { gridControl1.EndUpdate(); }VB.NETgridControl1.BeginUpdate() Try gridView1.Columns.Clear() gridControl1.DataSource Nothing gridControl1.DataSource (Of newDataSource ) Finally gridControl1.EndUpdate() End Try将更改发布到源对于大多数数据源类型数据网格允许用户在运行时编辑数据但这些更改不会自动保存到底层数据源中。请参考以下主题以了解如何将更改提交到数据源Post Data to an Underlying Data Source。清除网格将GridControl.DataSource属性设置为 null在 VB.NET 中为 Nothing然后调用视图的 Columns.Clear()方法。C#gridControl1.DataSource null; gridView1.Columns.Clear();VB.NETgridControl1.DataSource Nothing gridView1.Columns.Clear()如果您需要暂时清除数据网格中的数据并随后再恢复其数据可以将该控件切换至一个空视图模式。C#using DevExpress.XtraGrid.Views.Grid; object ds; // Data source // Clear ds gridControl1.DataSource; gridControl1.DataSource null; GridView view new GridView(gridControl1); view.OptionsView.ShowGroupPanel false; view.OptionsView.ShowColumnHeaders false; gridControl1.MainView view; // Restore gridControl1.MainView gridView1; gridControl1.DataSource ds;VB.NETImports DevExpress.XtraGrid.Views.Grid Private ds As Object Data source Clear ds gridControl1.DataSource gridControl1.DataSource Nothing Dim view As New GridView(gridControl1) view.OptionsView.ShowGroupPanel False view.OptionsView.ShowColumnHeaders False gridControl1.MainView view Restore gridControl1.MainView gridView1 gridControl1.DataSource ds支持Fluent API传统的自定义网格列或将它们与数据绑定的方法是假定您通过字符串字段名来访问列如果无法在运行时填充网格列或者您需要自定义逻辑来获取列设置那么可以使用一个简洁的 API 替代它。在 Visual Studio 中智能感知功能允许您查看所有数据源属性并将所需的字段绑定到网格列上。这种技术在您修改数据源结构例如删除一个数据源字段或者在字段名称中出现拼写错误比如试图访问一个不存在的字段时会生成防故障代码这些操作会立即引发无法被忽略的异常。而传统的技术在某些情况下可能会忽略此类错误从而生成“无效”代码这些代码难以被检测到。要使用此功能请在您的代码中引用XtraGrid.Extension命名空间。C#using DevExpress.XtraGrid.Extensions;VB.NETImports DevExpress.XtraGrid.Extensions现在您可以调用ColumnView.With方法来添加和配置列以下代码片段展示了示例内容C#gridView1.WithCustomer(settings { settings.Columns .Add(f f.ContactName, col { col.Caption Contact; col.SortOrder DevExpress.Data.ColumnSortOrder.Descending; col.SortIndex 0; }) .Add((f f.Phone), c { c.Caption Phone Number; }) .Add(p p.CompanyName) .WithCaption(Company Name) .WithGrouping() .With(col { }); });VB.NETgridView1.With(Of Customer)(Sub(settings) settings.Columns.Add(Function(f) f.ContactName, Sub(col) col.Caption Contact col.SortOrder DevExpress.Data.ColumnSortOrder.Descending col.SortIndex 0 End Sub).Add((Function(f) f.Phone), Sub(c) c.Caption Phone Number).Add(Function(p) p.CompanyName).WithCaption(Company Name).WithGrouping().With(Sub(col) End Sub) End Sub)借助fluent API您可以使用ColumnView.GetColumnViewSettings方法来获取现有列并修改其设置。例如以下代码是 GridView.RowCellStyle事件处理程序它会识别与行单元格相关联的列并如果该列是“Phone”则将其填充为红色C#void gridView1_RowCellStyle(object sender, DevExpress.XtraGrid.Views.Grid.RowCellStyleEventArgs e) { GridColumn col (sender as ColumnView).GetColumnViewSettingsCustomer().Columns[c c.Phone].AsColumn(); if((col!null) (e.Column col)) { e.Appearance.BackColor Color.Red; } }VB.NETPrivate Sub gridView1_RowCellStyle(ByVal sender As Object, ByVal e As DevExpress.XtraGrid.Views.Grid.RowCellStyleEventArgs) Dim col As GridColumn (TryCast(sender, ColumnView)).GetColumnViewSettings(Of Customer)().Columns(Function(c) c.Phone).AsColumn() If (col IsNot Nothing) AndAlso (e.Column Is col) Then e.Appearance.BackColor Color.Red End If End Sub