文章出處

C1FlexGrid.Subtotal方法可以增加包含普通(非小計)行的匯總數據的分類匯總行。

分類匯總支持分層聚合。例如,如果你的表格包含銷售數據,你可能會通過產品、地區和推銷員來小計一下以得出總的銷售數字。下面的代碼說明了這一點:

  1:         private void UpdateTotals()
  2:         {
  3:             // clear existing totals
  4:             _flex.Subtotal(AggregateEnum.Clear);
  5: 
  6:             // calculate subtotals (three levels, totals on every column)
  7:             for (int c = 3; c < _flex.Cols.Count; c++)
  8:             {
  9:                 if (_chkGrand.Checked)
 10:                     _flex.Subtotal(AggregateEnum.Sum, 0, -1, c, "各類產品合計");
 11:                 if (_chkDirection.Checked)
 12:                     _flex.Subtotal(AggregateEnum.Sum, 1, 1, c, "{0}合計");
 13:                 if (_chkRegion.Checked)
 14:                     _flex.Subtotal(AggregateEnum.Sum, 2, 2, c, "{0}方區域合計");
 15:             }
 16: 
 17:             // done, autosize columns to finish
 18:             _flex.AutoSizeCols();
 19:         }

 

 

代碼中重要接口說明:

_flex.Tree.Column:獲取或設置樹形結構在哪一列顯示。

_flex.Subtotal(AggregateEnum.Sum, -1, -1, 3, "Grand Total");

參數1:同時方式

參數2:當前樹節點的級別,-1 為最外層節點

參數3:用于獲取分組信息的列

參數4:統計的列

參數5:分組說明文本信息

即上述代碼設置了三層分組,第一層為所有銷售信息的匯總。第二層以產品類別分組,第三層以區域信息進行分組。

 

當C1FlexGrid.Subtotal方法添加了匯總信息行,它會自動分配匯總樣式到新的行(有五個層級的分類匯總內置樣式)。你可以使用“樣式編輯器”或代碼在設計器中改變大綱樣式的屬性,以此來自定義分類匯總行的外觀。例如:

  1: // 設置分類匯總的樣式。
  2: CellStyle cs; 
  3: cs = _flex.Styles[CellStyleEnum.GrandTotal]; 
  4: cs.BackColor = Color.Black; 
  5: cs.ForeColor = Color.White; 
  6: cs.Font = new Font(Font, FontStyle.Bold); 
  7: cs = _flex.Styles[CellStyleEnum.Subtotal0]; 
  8: cs.BackColor = Color.DarkRed; 
  9: cs.ForeColor = Color.White; 
 10: cs.Font = new Font(Font, FontStyle.Bold); 
 11: cs = _flex.Styles[CellStyleEnum.Subtotal1]; 
 12: cs.BackColor = Color.DarkBlue; 
 13: cs.ForeColor = Color.White; 
 14: 

 

執行此代碼后,表格看起來是這樣的:

image

除了總量之外,你也可以計算其他分類匯總(例如,平均值或百分比),并計算每一行的幾個匯總(例如,毛銷售額及凈銷售額)。

Demo 下載:

VS2010 + C# + Framework 4.0 + Studio for Winforms 2013V3 點擊下載

VS2010 + VB.NET + Framework 4.0 + Studio for Winforms 2013V3 點擊下載


文章列表




Avast logo

Avast 防毒軟體已檢查此封電子郵件的病毒。
www.avast.com


arrow
arrow
    全站熱搜
    創作者介紹
    創作者 大師兄 的頭像
    大師兄

    IT工程師數位筆記本

    大師兄 發表在 痞客邦 留言(0) 人氣()