zhouxuxuxiu 发表于 2015-9-24 10:49:50

DevExpress如何实现自定义求和

概述:DevExpress实现自定义求和的效果图及事例代码

自定义求和的效果图如下:http://image.evget.com/images/article/2012102201.jpg代码如下:C#
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
using DevExpress.XtraPivotGrid;

fieldExtendedPrice.Caption = "Percentage of Orders over $500";
// Enable a custom summary calculation for the Extended Price field.
fieldExtendedPrice.SummaryType = DevExpress.Data.PivotGrid.PivotSummaryType.Custom;
// Specify the settings used to format values.
fieldExtendedPrice.CellFormat.FormatType = DevExpress.Utils.FormatType.Numeric;
fieldExtendedPrice.CellFormat.FormatString = "p";

int minSum = 500;

private void pivotGridControl1_CustomSummary(object sender,
PivotGridCustomSummaryEventArgs e) {
   if(e.DataField != fieldExtendedPrice) return;
   // A variable which counts the number of orders whose sum exceeds $500.
   int order500Count = 0;
   // Get the record set corresponding to the current cell.
   PivotDrillDownDataSource ds = e.CreateDrillDownDataSource();
   // Iterate through the records and count the orders.
   for(int i = 0; i < ds.RowCount; i++) {
      PivotDrillDownDataRow row = ds;
      // Get the order's total sum.
      decimal orderSum = (decimal)row;
      if(orderSum >= minSum) order500Count ++;
   }
   // Calculate the percentage.
   if(ds.RowCount > 0) {
      e.CustomValue = (decimal)order500Count/ds.RowCount;
   }
}




VB
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
Imports DevExpress.XtraPivotGrid

fieldExtendedPrice.Caption = "Percentage of Orders over $500"
' Enable a custom summary calculation for the Extended Price field.
fieldExtendedPrice.SummaryType = DevExpress.Data.PivotGrid.PivotSummaryType.Custom
' Specify the settings used to format values.
fieldExtendedPrice.CellFormat.FormatType = DevExpress.Utils.FormatType.Numeric
fieldExtendedPrice.CellFormat.FormatString = "p"

Dim minSum As Integer = 500

Private Sub PivotGridControl1_CustomSummary(ByVal sender As Object, _
ByVal e As PivotGridCustomSummaryEventArgs) Handles PivotGridControl1.CustomSummary
   If Not e.DataField Is fieldExtendedPrice Then Return
   ' A variable which counts the number of orders whose sum exceeds $500.
   Dim order500Count As Integer = 0
   ' Get the record set corresponding to the current cell.
   Dim ds As PivotDrillDownDataSource = e.CreateDrillDownDataSource()
   ' Iterate through the records and count the orders.
   Dim i As Integer
   For i = 0 To ds.RowCount - 1
      Dim row As PivotDrillDownDataRow = ds(i)
      ' Get the order's total sum.
      Dim orderSum As Decimal = row(fieldExtendedPrice)
      If orderSum >= minSum Then order500Count = order500Count + 1
   Next
   ' Calculate the percentage.
   If ds.RowCount > 0 Then
      e.CustomValue = order500Count / ds.RowCount
   End If
End Sub


更多资讯





bingw001 发表于 2015-9-24 22:51:13

晕,这可怎么看啦

zhouxuxuxiu 发表于 2015-9-25 09:39:17

bingw001 发表于 2015-9-24 22:51
晕,这可怎么看啦

呀,居然看不见了
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
using DevExpress.XtraPivotGrid;

fieldExtendedPrice.Caption = "Percentage of Orders over $500";
// Enable a custom summary calculation for the Extended Price field.
fieldExtendedPrice.SummaryType = DevExpress.Data.PivotGrid.PivotSummaryType.Custom;
// Specify the settings used to format values.
fieldExtendedPrice.CellFormat.FormatType = DevExpress.Utils.FormatType.Numeric;
fieldExtendedPrice.CellFormat.FormatString = "p";

int minSum = 500;

private void pivotGridControl1_CustomSummary(object sender,
PivotGridCustomSummaryEventArgs e) {
   if(e.DataField != fieldExtendedPrice) return;
   // A variable which counts the number of orders whose sum exceeds $500.
   int order500Count = 0;
   // Get the record set corresponding to the current cell.
   PivotDrillDownDataSource ds = e.CreateDrillDownDataSource();
   // Iterate through the records and count the orders.
   for(int i = 0; i < ds.RowCount; i++) {
      PivotDrillDownDataRow row = ds;
      // Get the order's total sum.
      decimal orderSum = (decimal)row;
      if(orderSum >= minSum) order500Count ++;
   }
   // Calculate the percentage.
   if(ds.RowCount > 0) {
      e.CustomValue = (decimal)order500Count/ds.RowCount;
   }
}
VB

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
Imports DevExpress.XtraPivotGrid

fieldExtendedPrice.Caption = "Percentage of Orders over $500"
' Enable a custom summary calculation for the Extended Price field.
fieldExtendedPrice.SummaryType = DevExpress.Data.PivotGrid.PivotSummaryType.Custom
' Specify the settings used to format values.
fieldExtendedPrice.CellFormat.FormatType = DevExpress.Utils.FormatType.Numeric
fieldExtendedPrice.CellFormat.FormatString = "p"

Dim minSum As Integer = 500

Private Sub PivotGridControl1_CustomSummary(ByVal sender As Object, _
ByVal e As PivotGridCustomSummaryEventArgs) Handles PivotGridControl1.CustomSummary
   If Not e.DataField Is fieldExtendedPrice Then Return
   ' A variable which counts the number of orders whose sum exceeds $500.
   Dim order500Count As Integer = 0
   ' Get the record set corresponding to the current cell.
   Dim ds As PivotDrillDownDataSource = e.CreateDrillDownDataSource()
   ' Iterate through the records and count the orders.
   Dim i As Integer
   For i = 0 To ds.RowCount - 1
      Dim row As PivotDrillDownDataRow = ds(i)
      ' Get the order's total sum.
      Dim orderSum As Decimal = row(fieldExtendedPrice)
      If orderSum >= minSum Then order500Count = order500Count + 1
   Next
   ' Calculate the percentage.
   If ds.RowCount > 0 Then
      e.CustomValue = order500Count / ds.RowCount
   End If
End Sub

bingw001 发表于 2015-9-25 09:51:15

哇,谢谢楼主给修复了
页: [1]
查看完整版本: DevExpress如何实现自定义求和