本主题描述设置汇总格式的要点。 描述了可以设置汇总文本格式以及改变汇总值外观的方式。
设置汇总文本的格式
总体汇总 和 分组汇总 值被分别按照 TreeListColumn.SummaryFooterStrFormat 和 TreeListColumn.RowFooterSummaryStrFormat 属性设置格式。 这些属性定义了汇总值的可以使用下列结构指定的格式:
<自定义文本>{0:<格式说明符><精度说明符>}<自定义文本>
下面的插图展示了如何通过 XtraTreeList 设计器 把自定义格式设置指派到分组汇总值和总体汇总值。

可以使用下列代码来达到相同的目的。
C# | ![]() |
---|---|
using DevExpress.XtraTreeList; using DevExpress.XtraTreeList.Columns; // ... TreeListColumn column = treeList1.Columns["Budget"]; column.SummaryFooter = SummaryItemType.Sum; column.SummaryFooterStrFormat = "Total: {0:c0}"; column.RowFooterSummary = SummaryItemType.Sum; column.RowFooterSummaryStrFormat = "Subtotal: {0:c0}"; |
Visual Basic | ![]() |
---|---|
Imports DevExpress.XtraTreeList Imports DevExpress.XtraTreeList.Columns ' ... Dim Column As TreeListColumn = TreeList1.Columns("Budget") Column.SummaryFooter = SummaryItemType.Sum Column.SummaryFooterStrFormat = "Total: {0:c0}" Column.RowFooterSummary = SummaryItemType.Sum Column.RowFooterSummaryStrFormat = "Subtotal: {0:c0}" |
下面的插图展示了运行结果。

我们考虑另一个设置汇总显示文本格式的示例。 下面的代码为 Last Order 列应用了汇总,并且设置结果日期/时间值的格式。 它把 TreeListColumn.SummaryFooterStrFormat 属性设置为 Maximum month: {0:MMMM yyyy} 字符串。 因此,相应的脚注单元格将显示月份名称和四位数字格式的年份。
C# | ![]() |
---|---|
using DevExpress.XtraTreeList; using DevExpress.XtraTreeList.Columns; // ... TreeListColumn column = treeList1.Columns["LastOrder"]; column.SummaryFooter = SummaryItemType.Max; column.SummaryFooterStrFormat = "Maximum month: {0:MMMM yyyy}"; |
Visual Basic | ![]() |
---|---|
Imports DevExpress.XtraTreeList Imports DevExpress.XtraTreeList.Columns ' ... Dim Column As TreeListColumn = TreeList1.Columns("LastOrder") Column.SummaryFooter = SummaryItemType.Max Column.SummaryFooterStrFormat = "Last month: {0:MMMM yyyy}" |
结果如下图所示。

改变汇总值的外观
除了为汇总值应用自定义格式以外,使用 XtraTreeList 控件还可以根据脚注单元格的显示值改变其外观。 例如,可以使用不同的颜色显示不同范围内的取值。 通过接管 TreeList.CustomDrawFooterCell 和 TreeList.CustomDrawRowFooterCell 事件可以完成此任务。 这些事件允许根据单元格的显示值来自定义绘制单元格 (请参阅 自定义绘制 主题获知细节)。
下面的示例代码为 Budget 列中的行脚注单元格应用了附条件的格式设置。 显示值大于 1,000,000 的单元格,以及显示值不于此数值的单元格被以不同的方式设置格式。 接管了 TreeList.CustomDrawRowFooterCell 事件来执行此任务。
C# | ![]() |
---|---|
private void treeList1_CustomDrawRowFooterCell(object sender, DevExpress.XtraTreeList.CustomDrawRowFooterCellEventArgs e) { if (e.Text == String.Empty || e.Column.Caption != "Budget") return; Brush brush; Border3DStyle borderStyle; // obtaining the summary value int summaryValue = Convert.ToInt32(e.Text); // specifying style characteristics for values outside the range if (summaryValue > 1000000) { brush = new LinearGradientBrush(e.Bounds, Color.FromArgb(0, 255, 128,0), Color.FromArgb(100, Color.Red), LinearGradientMode.Vertical); borderStyle = Border3DStyle.RaisedInner; } else { brush = new LinearGradientBrush(e.Bounds, Color.FromArgb(100, Color.Blue), Color.FromArgb(0, 255, 128,0), LinearGradientMode.Vertical); borderStyle = Border3DStyle.SunkenOuter; } // filling the background and painting borders using(brush) { e.Graphics.FillRectangle(brush, e.Bounds); } ControlPaint.DrawBorder3D(e.Graphics, e.Bounds, borderStyle); // formatting and painting the string string outText = string.Format("Subtotal: {0:c0}", summaryValue); e.Graphics.DrawString(outText, e.Appearance.Font, e.Appearance.GetForeBrush(e.Cache), e.Bounds, e.Appearance.GetStringFormat()); // prohibiting default painting e.Handled = true; } |
Visual Basic | ![]() |
---|---|
Private Sub TreeList1_CustomDrawRowFooterCell(ByVal sender As Object, ByVal e As DevExpress.XtraTreeList.CustomDrawRowFooterCellEventArgs) _ Handles TreeList1.CustomDrawRowFooterCell If e.Text = String.Empty Or e.Column.Caption <> "Budget" Then Exit Sub Dim CellBrush As Brush Dim CellBorderStyle As Border3DStyle ' obtaining the summary value Dim SummaryValue As Integer = Convert.ToInt32(e.Text) ' specifying style characteristics for values outside the range If SummaryValue > 1000000 Then CellBrush = New LinearGradientBrush(e.Bounds, Color.FromArgb(0, 255, 128, 0), Color.FromArgb(100, Color.Red), LinearGradientMode.Vertical) CellBorderStyle = Border3DStyle.RaisedInner Else CellBrush = New LinearGradientBrush(e.Bounds, Color.FromArgb(100, Color.Blue), Color.FromArgb(0, 255, 128, 0), LinearGradientMode.Vertical) CellBorderStyle = Border3DStyle.SunkenOuter End If ' filling the background and painting borders e.Graphics.FillRectangle(CellBrush, e.Bounds) CellBrush.Dispose() ControlPaint.DrawBorder3D(e.Graphics, e.Bounds, CellBorderStyle) ' formatting and painting the string Dim OutText As String = String.Format("Subtotal: {0:c0}", SummaryValue) Dim BoundsF As New RectangleF(e.Bounds.X, e.Bounds.Y, e.Bounds.Width, e.Bounds.Height) e.Graphics.DrawString(OutText, e.Appearance.Font, e.Appearance.GetForeBrush(e.Cache), BoundsF, e.Appearance.GetStringFormat()) ' prohibiting default painting e.Handled = True End Sub |
注意,Budget 列的 TreeListColumn.RowFooterSummaryStrFormat 属性必须被设置为 {0}。 否则示例代码不能实现适当的功能。
下面的插图展示了运行结果。
