Devexpress Gridview 自定義彙總CustomSummaryCalculate(加權平均)

来源:http://www.cnblogs.com/flysong/archive/2017/12/04/7979655.html
-Advertisement-
Play Games

Devexpress Gridview 提供了簡單的求和,平均等方法,複雜的彙總方法則需要自定義,使用gridview 的CustomSummaryCalculate 事件,根據官網的文檔及各論壇案例實現加權平均的方法。 自定義彙總方法(加權平均) 參考資料: https://documentati ...


        Devexpress Gridview 提供了簡單的求和,平均等方法,複雜的彙總方法則需要自定義,使用gridview 的CustomSummaryCalculate 事件,根據官網的文檔及各論壇案例實現加權平均的方法。 

gridView1.CustomSummaryCalculate += new CustomSummaryEventHandler(view_CustomSummaryCalculate);

  自定義彙總方法(加權平均)

 
 3 // 顯示記錄數量
 4 GridColumnSummaryItem siTotal = new GridColumnSummaryItem();
 5 siTotal.SummaryType = SummaryItemType.Count;
 6 siTotal.DisplayFormat = "{0} records";
 7 
 8 // 對 Length 欄位列 彙總平均    
 9 GridColumnSummaryItem siAverage = new GridColumnSummaryItem();
10 siAverage.SummaryType = SummaryItemType.Average;
11 siAverage.FieldName = "Length";
12 siAverage.DisplayFormat = "Average: {0:#.#}"; 
14     
15 // gridView1中colIdRandom 列顯示多行彙總:最小、計數、加權平均    
16 this.colIdRandom.Summary.AddRange(new DevExpress.XtraGrid.GridSummaryItem[] {
17      new DevExpress.XtraGrid.GridColumnSummaryItem(DevExpress.Data.SummaryItemType.Min, "IdRandom", "MIN={0}", "Min"),
18      new DevExpress.XtraGrid.GridColumnSummaryItem(DevExpress.Data.SummaryItemType.Count, "IdRandom", "Count = {0}", "Count"),
19      new DevExpress.XtraGrid.GridColumnSummaryItem(DevExpress.Data.SummaryItemType.Custom, "IdRandom", "Custom Summary = {0}")});
20 // 計算加權平均    
21 CalacWeightAverage(gridView1,"WeightColumn","TEST1",colIdRandom.FieldName);
22 
23 // 彙總項目綁定到相應列,顯示在view footer
24 gridView1.Columns["ID"].SummaryItem.Collection.Add(siTotal);
25 gridView1.Columns["Length"].SummaryItem.Collection.Add(siAverage);
26 gridView1.OptionsView.ShowFooter = true;
27 
28 
29 double sumWt=0;
30 double customSummaryValue;
31 string WeightColumn;
32 
33 void CalacWeightAverage(DevExpress.XtraGrid.Views.Grid.GridView view, string weightColumn, params string[] weightAverageColumns)
34 {    
35     WeightColumn = weightColumn;
36     if (view.Columns.ColumnByFieldName(weightColumn) != null)
37     {
38         if (!(view.Columns[weightColumn].ColumnType != typeof(decimal)) 
39             || !(view.Columns[weightColumn].ColumnType != typeof(int)))
40         {
41             for (int i = 0; i < weightAverageColumns.Length; i++)
42             {
43                 string fieldName = weightAverageColumns[i];
44                 if (view.Columns.ColumnByFieldName(fieldName) != null 
45                     && (!(view.Columns[fieldName].ColumnType != typeof(decimal)) 
46                     || !(view.Columns[fieldName].ColumnType != typeof(int))))
47                 {
48                     view.Columns[fieldName].SummaryItem.SummaryType = SummaryItemType.Custom;
49                     GridSummaryItem gridSummaryItem = view.GroupSummary.Add(SummaryItemType.Custom, fieldName, view.Columns[fieldName]);
50                     gridSummaryItem.DisplayFormat = view.Columns[fieldName].SummaryItem.DisplayFormat;
51                 }
52             }
53         //gridView1.CustomSummaryCalculate += new CustomSummaryEventHandler(gridView1_CustomSummaryCalculate);    
54         }
55     }
56 }
57 
// 自定義彙總演算法 58 void gridView1_CustomSummaryCalculate(object sender, CustomSummaryEventArgs e) 59 { 60 if (e.Item != null) 61 { 62 //if (e.IsGroupSummary) {} 分組彙總 63 //if (e.IsTotalSummary) {} 全部彙總 64 65 var gridView = sender as DevExpress.XtraGrid.Views.Grid.GridView; 66 if (gridView.Columns.ColumnByFieldName(WeightColumn) != null) 67 { 68 GridSummaryItem gridSummaryItem = e.Item as GridSummaryItem; 69 if (e.SummaryProcess == CustomSummaryProcess.Start) 70 { 71 customSummaryValue = 0; 72 sumWt = 0; 73 } 74 else if (e.SummaryProcess == CustomSummaryProcess.Calculate) 75 { 76 if (e.FieldValue != null && e.FieldValue != DBNull.Value) 77 { 78 sumWt += Convert.ToDecimal(gridView.GetRowCellValue(e.RowHandle, WeightColumn)); 79 customSummaryValue += Convert.ToDecimal(e.FieldValue) * Convert.ToDecimal(gridView.GetRowCellValue(e.RowHandle, WeightColumn)); 80 } 81 } 82 else if (e.SummaryProcess == CustomSummaryProcess.Finalize) 83 { 84 e.TotalValue = ((sumWt == 0) ? 0 : (customSummaryValue / sumWt)); 85 } 86 } 87 } 88 }

 

參考資料:

https://documentation.devexpress.com/WindowsForms/701/Controls-and-Libraries/Data-Grid/Summaries/Working-with-Summaries-in-Code

https://documentation.devexpress.com/CoreLibraries/DevExpress.Data.CustomSummaryEventArgs.class

https://www.cnblogs.com/EasyInvoice/p/3892136.html


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 上一章介紹了Python的相關知識,本章就開始著手操作,創建我們的第一個Python程式,首先需要配置好Python的運行環境 1.python環境準備 1.1.windows下安裝 Windows10配置環境變數,防止程式調用出錯 如:我的路徑為D:\Program Files (x86)\pyt ...
  • 說明 本文寫於2017 11 16,使用Go 1.9.2,操作系統為CentOS 7。 官方文檔參看 "https://golang.org/doc/install?download=go1.9.2.linux amd64.tar.gz" 。 安裝 上述步驟在 中增加一行: 環境變數 Go預設使用 ...
  • 1. Myeclipse自帶有Tomcat服務和預覽網頁的功能,非常的方便快捷,但是有時候在運行項目會出現一些非代碼錯誤的問題,這種問題不是由於代碼錯誤引起的,使用外部的瀏覽器瀏覽項目的網頁就正常顯示,一般建議使用外部的Tomcat伺服器和瀏覽器。 2.首先下載Tomcat,百度一搜即可下載,下解壓 ...
  • 沒學指針之前如何操作? 用一個臨時變數進行交換 學習指針的方法之後,如何操作? 把指針作為函數參數的方法處理從大到小排序問題。 ...
  • 就業形勢嚴峻的情況下,每個企業對於人才的需求都不一樣,並不是說公司不願意招聘培訓班出來的人,而是看你的能力是不是能勝任企業招聘人才的需求,是不是能給企業帶來價值的人。 現在市面上的培訓機構多如牛毛,然後很多看到了互聯網企業的高薪都紛紛跑去學習軟體開發,不清楚自己到底是不是真的喜歡搞技術,還是為了一份 ...
  • 一、序言 大家或多或少都聽過WebService(Web服務),有一段時間很多電腦期刊、書籍和網站都大肆的提及和宣傳WebService技術,其中不乏很多吹噓和做廣告的成分。但是不得不承認的是WebService真的是一門新興和有前途的技術,那麼WebService到底是什麼?何時應該用? 當前的 ...
  • 下麵有一個字元串陣列: 要求是獲取元素最長或最短的長度。你可以在程式中創建一個對象,這個對象有兩個屬性元素值和元素長度: class Class6 { private string _ElementValue; public string ElementValue { get { return _E ...
  • 異常能清楚地將“普通程式”和“錯誤處理”分開了,這使得程式更容易理解。 代碼的可理解性應該是我們虔誠追求的目標。 ...
一周排行
    -Advertisement-
    Play Games
  • 示例項目結構 在 Visual Studio 中創建一個 WinForms 應用程式後,項目結構如下所示: MyWinFormsApp/ │ ├───Properties/ │ └───Settings.settings │ ├───bin/ │ ├───Debug/ │ └───Release/ ...
  • [STAThread] 特性用於需要與 COM 組件交互的應用程式,尤其是依賴單線程模型(如 Windows Forms 應用程式)的組件。在 STA 模式下,線程擁有自己的消息迴圈,這對於處理用戶界面和某些 COM 組件是必要的。 [STAThread] static void Main(stri ...
  • 在WinForm中使用全局異常捕獲處理 在WinForm應用程式中,全局異常捕獲是確保程式穩定性的關鍵。通過在Program類的Main方法中設置全局異常處理,可以有效地捕獲並處理未預見的異常,從而避免程式崩潰。 註冊全局異常事件 [STAThread] static void Main() { / ...
  • 前言 給大家推薦一款開源的 Winform 控制項庫,可以幫助我們開發更加美觀、漂亮的 WinForm 界面。 項目介紹 SunnyUI.NET 是一個基於 .NET Framework 4.0+、.NET 6、.NET 7 和 .NET 8 的 WinForm 開源控制項庫,同時也提供了工具類庫、擴展 ...
  • 說明 該文章是屬於OverallAuth2.0系列文章,每周更新一篇該系列文章(從0到1完成系統開發)。 該系統文章,我會儘量說的非常詳細,做到不管新手、老手都能看懂。 說明:OverallAuth2.0 是一個簡單、易懂、功能強大的許可權+可視化流程管理系統。 有興趣的朋友,請關註我吧(*^▽^*) ...
  • 一、下載安裝 1.下載git 必須先下載並安裝git,再TortoiseGit下載安裝 git安裝參考教程:https://blog.csdn.net/mukes/article/details/115693833 2.TortoiseGit下載與安裝 TortoiseGit,Git客戶端,32/6 ...
  • 前言 在項目開發過程中,理解數據結構和演算法如同掌握蓋房子的秘訣。演算法不僅能幫助我們編寫高效、優質的代碼,還能解決項目中遇到的各種難題。 給大家推薦一個支持C#的開源免費、新手友好的數據結構與演算法入門教程:Hello演算法。 項目介紹 《Hello Algo》是一本開源免費、新手友好的數據結構與演算法入門 ...
  • 1.生成單個Proto.bat內容 @rem Copyright 2016, Google Inc. @rem All rights reserved. @rem @rem Redistribution and use in source and binary forms, with or with ...
  • 一:背景 1. 講故事 前段時間有位朋友找到我,說他的窗體程式在客戶這邊出現了卡死,讓我幫忙看下怎麼回事?dump也生成了,既然有dump了那就上 windbg 分析吧。 二:WinDbg 分析 1. 為什麼會卡死 窗體程式的卡死,入口門檻很低,後續往下分析就不一定了,不管怎麼說先用 !clrsta ...
  • 前言 人工智慧時代,人臉識別技術已成為安全驗證、身份識別和用戶交互的關鍵工具。 給大家推薦一款.NET 開源提供了強大的人臉識別 API,工具不僅易於集成,還具備高效處理能力。 本文將介紹一款如何利用這些API,為我們的項目添加智能識別的亮點。 項目介紹 GitHub 上擁有 1.2k 星標的 C# ...