原創|其它|編輯:郝浩|2012-11-09 17:38:01.000|閱讀 1004 次
概述:下面的示例演示如何運用DevExpress XtraPivotGrid分割字段值單元格。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
下面的示例演示如何運用DevExpress XtraPivotGrid分割字段值單元格。
在這個例子中,我們運用CustomFieldValueCells事件參數的Split方法來將總計列標題( Grand Total )分成兩個單元格: Price 和Count。新創建的單元格的數量,大小和標題都由FieldValueSplitData對象指定。
示例代碼如下:
C# using System; using System.Collections.Generic; using System.Globalization; using System.Windows.Forms; using DevExpress.XtraPivotGrid; using DevExpress.XtraPivotGrid.Data; namespace XtraPivotGrid_SplittingCells { public partial class Form1 : Form { public Form1() { InitializeComponent(); pivotGridControl1.CustomFieldValueCells += new PivotCustomFieldValueCellsEventHandler(pivotGrid_CustomFieldValueCells); } void Form1_Load(object sender, EventArgs e) { PivotHelper.FillPivot(pivotGridControl1); pivotGridControl1.DataSource = PivotHelper.GetDataTable(); pivotGridControl1.BestFit(); } protected void pivotGrid_CustomFieldValueCells(object sender, PivotCustomFieldValueCellsEventArgs e) { if (pivotGridControl1.DataSource == null) return; if (radioGroup1.SelectedIndex == 0) return; // Creates a predicate that returns true for the Grand Total // headers, and false for any other column/row header. // Only cells that match this predicate are split. Predicate<FieldValueCell> condition = new Predicate<FieldValueCell>(delegate(FieldValueCell matchCell) { return matchCell.ValueType == PivotGridValueType.GrandTotal && matchCell.Field == null; }); // Creates a list of cell definitions that represent newly created cells. // Two definitions are added to the list. The first one identifies // the Price cell, which has two nested cells (the Retail Price and Wholesale Price // data field headers). The second one identifies the Count cell with // one nested cell (the Quantity data field header). List<FieldValueSplitData> cells = new List<FieldValueSplitData>(2); cells.Add(new FieldValueSplitData("Price", 2)); cells.Add(new FieldValueSplitData("Count", 1)); // Performs splitting. e.Split(true, condition, cells); } void pivotGridControl1_FieldValueDisplayText(object sender, PivotFieldDisplayTextEventArgs e) { if(e.Field == pivotGridControl1.Fields[PivotHelper.Month]) { e.DisplayText = CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName((int)e.Value); } } void radioGroup1_SelectedIndexChanged(object sender, EventArgs e) { this.pivotGridControl1.LayoutChanged(); pivotGridControl1.BestFit(); } } }
VB Imports Microsoft.VisualBasic Imports System Imports System.Collections.Generic Imports System.Globalization Imports System.Windows.Forms Imports DevExpress.XtraPivotGrid Imports DevExpress.XtraPivotGrid.Data Namespace XtraPivotGrid_SplittingCells Partial Public Class Form1 Inherits Form Public Sub New() InitializeComponent() AddHandler pivotGridControl1.CustomFieldValueCells, _ AddressOf pivotGrid_CustomFieldValueCells End Sub Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load PivotHelper.FillPivot(pivotGridControl1) pivotGridControl1.DataSource = PivotHelper.GetDataTable() pivotGridControl1.BestFit() End Sub Protected Sub pivotGrid_CustomFieldValueCells(ByVal sender As Object, _ ByVal e As PivotCustomFieldValueCellsEventArgs) If pivotGridControl1.DataSource Is Nothing Then Return End If If radioGroup1.SelectedIndex = 0 Then Return End If ' Creates a predicate that returns true for the Grand Total ' headers, and false for any other column/row header. ' Only cells that match this predicate are split. Dim condition As New Predicate(Of FieldValueCell)(AddressOf AnonymousMethod1) ' Creates a list of cell definitions that represent newly created cells. ' Two definitions are added to the list. The first one identifies ' the Price cell, which has two nested cells (the Retail Price and Wholesale Price ' data field headers). The second one identifies the Count cell with ' one nested cell (the Quantity data field header). Dim cells As New List(Of FieldValueSplitData)(2) cells.Add(New FieldValueSplitData("Price", 2)) cells.Add(New FieldValueSplitData("Count", 1)) ' Performs splitting. e.Split(True, condition, cells) End Sub Private Function AnonymousMethod1(ByVal matchCell As FieldValueCell) As Boolean Return matchCell.ValueType = PivotGridValueType.GrandTotal AndAlso _ matchCell.Field Is Nothing End Function Private Sub pivotGridControl1_FieldValueDisplayText(ByVal sender As Object, _ ByVal e As PivotFieldDisplayTextEventArgs) _ Handles pivotGridControl1.FieldValueDisplayText If Object.Equals(e.Field, pivotGridControl1.Fields(PivotHelper.Month)) Then e.DisplayText = _ CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(CInt(Fix(e.Value))) End If End Sub Private Sub radioGroup1_SelectedIndexChanged(ByVal sender As Object, _ ByVal e As EventArgs) _ Handles radioGroup1.SelectedIndexChanged Me.pivotGridControl1.LayoutChanged() pivotGridControl1.BestFit() End Sub End Class End Namespace
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn
文章轉載自:慧都控件網