我们考虑一个通过 ColumnView.CustomColumnDisplayText 事件定制单元格显示文本的示例。 假设网格的视图中包含 CurrencyType 和 Price 列。 如果货币类型等于 0,则以美元格式显示价格。 如果货币类型等于 1,则以欧元格式显示价格。 在 ColumnView.CustomColumnDisplayText 事件处理程序中,获取了 CurrencyType 和 Price 列的取值。 然后根据货币类型设置价格的格式,并把已设置格式的字符串指派到事件的 DisplayText 参数。
C# | 复制代码 |
---|---|
using DevExpress.XtraGrid.Views.Base; using System.Globalization; // The cultures used to format the values for the two different currencies. CultureInfo ciUSA = new CultureInfo("en-US"); CultureInfo ciEUR = new CultureInfo("fr-FR", false); private void gridView1_CustomColumnDisplayText(object sender, CustomColumnDisplayTextEventArgs e) { ColumnView View = sender as ColumnView; if(e.Column.FieldName == "Price") { int currencyType = (int)view.GetRowCellValue(e.RowHandle, View.Columns["CurrencyType"]); decimal price = (decimal)view.GetRowCellValue(e.RowHandle, View.Columns["Price"]); // Conditional formatting: switch(currencyType) { case 0: e.DisplayText = string.Format(ciUSA, "{0:c}", price); break; case 1: e.DisplayText = string.Format(ciEUR, "{0:c}", price); break; } } } |
Visual Basic | 复制代码 |
---|---|
Imports DevExpress.XtraGrid.Views.Base Imports System.Globalization ' The cultures used to format the values for the two different currencies. Dim ciUSA As CultureInfo = New CultureInfo("en-US") Dim ciEUR As CultureInfo = New CultureInfo("fr-FR", False) Private Sub GridView1_CustomColumnDisplayText(ByVal sender As Object, _ ByVal e As CustomColumnDisplayTextEventArgs) _ Handles GridView1.CustomColumnDisplayText Dim View As ColumnView = sender If e.Column.FieldName = "Price" Then Dim currencyType As Integer = View.GetRowCellValue(e.RowHandle, _ View.Columns("CurrencyType")) Dim price As Decimal = View.GetRowCellValue(e.RowHandle, View.Columns("Price")) ' Conditional formatting: Select Case currencyType Case 0 : e.DisplayText = String.Format(ciUSA, "{0:c}", price) Case 1 : e.DisplayText = String.Format(ciEUR, "{0:c}", price) End Select End If End Sub |