AABBbaby

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

统计

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

本教程将介绍:

  • 在设计时创建未绑定列
  • 在设计时为未绑定列指定表达式
  • 在运行时编辑表达式
  • 向代码中的未绑定列提供数据
  • 编辑未绑定列中的单元格值并保存更改

P.S:DevExpress WinForms拥有180+组件和UI库,能为Windows Forms平台创建具有影响力的业务解决方案。DevExpress WinForms能完美构建流畅、美观且易于使用的应用程序,无论是Office风格的界面,还是分析处理大批量的业务数据,它都能轻松胜任!

获取DevExpress WinForms v24.1正式版下载

DevExpress技术交流群10:532598169      欢迎一起进群讨论

起点

一个与Northwind数据库的“Order Details”表绑定的数据网格应用程序。

DevExpress WinForms中文教程图集
在设计时创建未绑定列

1. 打开DevExpress WinForms Data Grid的智能标记,单击Add Column来创建列。

DevExpress WinForms中文教程图集

2. 选择此列并设置其GridColumn.FieldName属性为唯一的字符串:“DiscountAmount”。

DevExpress WinForms中文教程图集

3. 将列的GridColumn.UnboundDataType属性设置为有效的数据类型。在本教程中,使用System.Decimal值。

DevExpress WinForms中文教程图集
在设计时指定表达式

1. 使用GridColumn.UnboundExpression 属性:单击省略号按钮打开Expression Editor(表达式编辑器)。

DevExpress WinForms中文教程图集

2. 创建一个简单表达式,乘以三个字段:“Quantity”, “Unit Price”, “Discount”。

DevExpress WinForms中文教程图集

3. 将OptionsColumn.AllowEdit属性设置为false来使该列只读。

DevExpress WinForms中文教程图集

TIP:将列的FormatInfo.FormatType设置为FormatType.Numeric,将FormatInfo.FormatString设置为c2,来将列值格式化为货币。

在运行时编辑未绑定列的表达式

将列的GridColumn.ShowUnboundExpressionMenu属性设置为true,来允许用户在运行时修改未绑定列的表达式。

DevExpress WinForms中文教程图集

用户可以在运行时从列的上下文菜单调用表达式编辑器来更改表达式。

DevExpress WinForms中文教程图集
向事件上的未绑定列提供数据

1. 创建另一个列,设置GridColumn.FieldName为Total,GridColumn.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.NET

Private 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#

Dictionary<int, decimal> customTotals = new Dictionary<int, 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.NET

Private 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

posted on   AABBbaby  阅读(14)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!
历史上的今天:
2020-10-30 Kendo UI ListView选择功能,让Web开发更轻松
2020-10-30 界面控件开发包DevExpress 2020年重大版本——v20.2震撼发布
2019-10-30 项目管理工具!DevExpress Winforms Gantt控件 v19.2强势来袭
2018-10-30 DevExpress v18.1新版亮点——XAF篇(一)
2017-10-30 【DevExpress v17.2新功能预告】改进DevExtreme编辑器
点击右上角即可分享
微信分享提示