ASPxGridView:添加一个非绑定列

转载自慧都控件网:http://www.evget.com/zh-CN/Info/catalog/18037.html

假设ASPxGridView网格控件被绑定到一个“Orders”数据表(NWIND数据库),该数据表中包含了"UnitPrice", "Quantity" 和 "Discount" 等字段。虽然,在该表中没有一个代表总和的字段,但是可以手动计算如下:UnitPrice*Quantity*(1-Discount)。该示例展示了如何添加一个非绑定列到ASPxGridView中,以展示订单SUM总和。

C#

using DevExpress.Web.ASPxGridView;
protected void ASPxGridView1_Init(object sender, EventArgs e) {
// Creates a column, customizes its settings and appends it to the Columns collection;
GridViewDataTextColumn colTotal = new GridViewDataTextColumn();
colTotal.Caption = "Total";
colTotal.FieldName = "Total";
colTotal.UnboundType = DevExpress.Data.UnboundColumnType.Integer;
colTotal.VisibleIndex = ASPxGridView1.VisibleColumns.Count;
colTotal.PropertiesTextEdit.DisplayFormatString = "c2";
ASPxGridView1.Columns.Add(colTotal);
}
// Populates the unbound column.
protected void ASPxGridView1_CustomUnboundColumnData(object sender,
ASPxGridViewColumnDataEventArgs e) {
if (e.Column.FieldName == "Total") {
decimal unitPrice = Convert.ToDecimal(e.GetListSourceFieldValue("UnitPrice"));
int quantity = Convert.ToInt32(e.GetListSourceFieldValue("Quantity"));
decimal discount = Convert.ToDecimal(e.GetListSourceFieldValue("Discount"));
e.Value = unitPrice * quantity * (1 - discount);
}

VB

Imports DevExpress.Web.ASPxGridView
Protected Sub ASPxGridView1_Init(ByVal sender As Object, ByVal e As System.EventArgs)_
Handles ASPxGridView1.Init
' Creates a column, customizes its settings and appends it to the Columns collection;
Dim colTotal As GridViewDataTextColumn = New GridViewDataTextColumn()
colTotal.Caption = "Total"
colTotal.FieldName = "Total"
colTotal.UnboundType = DevExpress.Data.UnboundColumnType.Integer
colTotal.VisibleIndex = ASPxGridView1.VisibleColumns.Count
colTotal.PropertiesTextEdit.DisplayFormatString = "c2"
ASPxGridView1.Columns.Add(colTotal)
End Sub
' Populates the unbound column.
Protected Sub ASPxGridView1_CustomUnboundColumnData(ByVal sender As Object,_
ByVal e As DevExpress.Web.ASPxGridView.ASPxGridViewColumnDataEventArgs)_
Handles ASPxGridView1.CustomUnboundColumnData
If e.Column.FieldName = "Total" Then
Dim unitPrice As Decimal = Convert.ToDecimal(e.GetListSourceFieldValue("UnitPrice"))
Dim quantity As Integer = Convert.ToInt32(e.GetListSourceFieldValue("Quantity"))
Dim discount As Decimal = Convert.ToDecimal(e.GetListSourceFieldValue("Discount"))
e.Value = unitPrice * quantity * (1 - discount)
End If
End Sub

结果如下:


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值