DevExpress WinForms中文教程:Data Grid - 如何创建未绑定列

📅 2026/7/6 20:49:00
DevExpress WinForms中文教程:Data Grid - 如何创建未绑定列
本教程将介绍在设计时创建未绑定列在设计时为未绑定列指定表达式在运行时编辑表达式向代码中的未绑定列提供数据编辑未绑定列中的单元格值并保存更改P.SDevExpress WinForms拥有180组件和UI库能为Windows Forms平台创建具有影响力的业务解决方案。DevExpress WinForms能完美构建流畅、美观且易于使用的应用程序无论是Office风格的界面还是分析处理大批量的业务数据它都能轻松胜任DevExpress新旧版本帮助文档下载欢迎进QQ qun获取169725316起点一个与Northwind数据库的“Order Details”表绑定的数据网格应用程序。在设计时创建未绑定列1. 打开​​​​​​​DevExpress WinForms Data Grid的智能标记单击Add Column来创建列。2. 选择此列并设置其GridColumn.FieldName属性为唯一的字符串“DiscountAmount”。3. 将列的GridColumn.UnboundDataType属性设置为有效的数据类型。在本教程中使用System.Decimal值。在设计时指定表达式1. 使用GridColumn.UnboundExpression 属性单击省略号按钮打开Expression Editor表达式编辑器。2. 创建一个简单表达式乘以三个字段“Quantity”, “Unit Price”, “Discount”。3. 将OptionsColumn.AllowEdit属性设置为false来使该列只读。TIP将列的FormatInfo.FormatType设置为FormatType.Numeric将FormatInfo.FormatString设置为c2来将列值格式化为货币。在运行时编辑未绑定列的表达式将列的GridColumn.ShowUnboundExpressionMenu属性设置为true来允许用户在运行时修改未绑定列的表达式。用户可以在运行时从列的上下文菜单调用表达式编辑器来更改表达式。向事件上的未绑定列提供数据1. 创建另一个列设置GridColumn.FieldName为TotalGridColumn.UnboundDataType为System.Decimal。2. 选择“gridView1”并在属性面板的“Events”页面上订阅ColumnView.CustomUnboundColumnData事件。注意ColumnView.CustomUnboundColumnData事件在每次即将显示列值时和修改列单元格后当需要发布数据时触发。3. 如果e.IsGetData事件参数为true则使用ColumnView.GetListSourceRowCellValue方法检索Quantity、UnitPrice和Discount列的值。计算未绑定列的值并将其分配给e.Value事件参数。C#void gridView_CustomUnboundColumnData(object sender, DevExpress.XtraGrid.Views.Base.CustomColumnDataEventArgs e) { GridView view sender as GridView; if(view null) return; int rowIndex e.ListSourceRowIndex; decimal unitPrice Convert.ToDecimal(view.GetListSourceRowCellValue(rowIndex, UnitPrice)); decimal quantity Convert.ToDecimal(view.GetListSourceRowCellValue(rowIndex, Quantity)); decimal discount Convert.ToDecimal(view.GetListSourceRowCellValue(rowIndex, Discount)); if (e.Column.FieldName ! Total) return; if (e.IsGetData) e.Value unitPrice * quantity * (1 - discount); }VB.NETPrivate Sub gridView_CustomUnboundColumnData(ByVal sender As Object, ByVal e As DevExpress.XtraGrid.Views.Base.CustomColumnDataEventArgs) Dim view As GridView TryCast(sender, GridView) If view Is Nothing Then Return End If Dim rowIndex As Integer e.ListSourceRowIndex Dim unitPrice As Decimal Convert.ToDecimal(view.GetListSourceRowCellValue(rowIndex, UnitPrice)) Dim quantity As Decimal Convert.ToDecimal(view.GetListSourceRowCellValue(rowIndex, Quantity)) Dim discount As Decimal Convert.ToDecimal(view.GetListSourceRowCellValue(rowIndex, Discount)) If e.Column.FieldName Total Then Return End If If e.IsGetData Then e.Value unitPrice * quantity * (1 - discount) End If End Sub编辑未绑定列中的单元格值在编辑时需要在未绑定列中保存更改。为此您可以使用ColumnView.CustomUnboundColumnData事件。下面的代码将Total列单元格中的更改保存到一个字典中e.IsSetData事件参数指示未绑定列中的单元格值是否被修改。C#Dictionaryint, decimal customTotals new Dictionaryint, decimal(); private void gridView1_CustomUnboundColumnData(object sender, DevExpress.XtraGrid.Views.Base.CustomColumnDataEventArgs e) { GridView view sender as GridView; if(view null) return; int rowIndex e.ListSourceRowIndex; decimal unitPrice Convert.ToDecimal(view.GetListSourceRowCellValue(rowIndex, UnitPrice)); decimal quantity Convert.ToDecimal(view.GetListSourceRowCellValue(rowIndex, Quantity)); decimal discount Convert.ToDecimal(view.GetListSourceRowCellValue(rowIndex, Discount)); if (e.Column.FieldName ! Total) return; if (e.IsGetData) { if (!customTotals.ContainsKey(rowIndex)) customTotals.Add(rowIndex, unitPrice * quantity * (1 - discount)); e.Value customTotals[rowIndex]; } if (e.IsSetData) { customTotals[rowIndex] Convert.ToDecimal(e.Value); } }VB.NETPrivate customTotals As New Dictionary(Of Integer, Decimal)() Private Sub gridView1_CustomUnboundColumnData(ByVal sender As Object, ByVal e As DevExpress.XtraGrid.Views.Base.CustomColumnDataEventArgs) Dim view As GridView TryCast(sender, GridView) If view Is Nothing Then Return End If Dim rowIndex As Integer e.ListSourceRowIndex Dim unitPrice As Decimal Convert.ToDecimal(view.GetListSourceRowCellValue(rowIndex, UnitPrice)) Dim quantity As Decimal Convert.ToDecimal(view.GetListSourceRowCellValue(rowIndex, Quantity)) Dim discount As Decimal Convert.ToDecimal(view.GetListSourceRowCellValue(rowIndex, Discount)) If e.Column.FieldName Total Then Return End If If e.IsGetData Then If Not customTotals.ContainsKey(rowIndex) Then customTotals.Add(rowIndex, unitPrice * quantity * (1 - discount)) End If e.Value customTotals(rowIndex) End If If e.IsSetData Then customTotals(rowIndex) Convert.ToDecimal(e.Value) End If End Sub