■ GridView 클래스의 CustomSummaryCalculate 이벤트를 사용해 SUMMARY 커스텀 계산하는 방법을 보여준다.
▶ MainForm.cs
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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 |
using System; using System.Collections.Generic; using System.Data; using DevExpress.Data; using DevExpress.XtraEditors; using DevExpress.XtraGrid.Columns; using DevExpress.XtraGrid; namespace TestProject { /// <summary> /// 메인 폼 /// </summary> public partial class MainForm : XtraForm { //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region Field /// <summary> /// 통화 컬럼 /// </summary> private GridColumn sourceCurrencyColumn = null; /// <summary> /// 값 컬럼 배열 /// </summary> private GridColumn[] sourceValueColumnArray = null; /// <summary> /// 통화 정렬 리스트 /// </summary> private SortedList<string, string> currencySortedList = new SortedList<string, string>(); /// <summary> /// 필드 요약 정렬 리스트 /// </summary> /// <remarks> /// SortedList(필드명, SortedList(통화코드, 요약 값)) /// </remarks> private SortedList<string, SortedList<string, decimal>> fieldSummarySortedList = new SortedList<string, SortedList<string, decimal>>(); #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - MainForm() /// <summary> /// 생성자 /// </summary> public MainForm() { InitializeComponent(); this.gridView.OptionsView.ShowFooter = true; this.gridView.OptionsView.ShowGroupPanel = false; this.gridView.OptionsView.ShowIndicator = false; this.gridView.OptionsView.ColumnAutoWidth = false; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Private //////////////////////////////////////////////////////////////////////////////// Event #region 폼 로드시 처리하기 - Form_Load(sender, e) /// <summary> /// 폼 로드시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void Form_Load(object sender, EventArgs e) { DataTable sampleTable = GetSampleList(); //////////////////////////////////////////////////////////////////////////////////////////////////// this.gridView.CustomSummaryCalculate += gridView_CustomSummaryCalculate; // 통화 컬럼을 설정한다. this.sourceCurrencyColumn = this.currencyColumn; // 값 컬럼 목록을 설정한다. this.sourceValueColumnArray = new GridColumn[] { this.amountColumn1 , this.amountColumn2 }; // 통화 목록을 설정한다. this.currencySortedList = GetCurrencySortedList(sampleTable); // 통화 컬럼과 값 컬럼 목록의 SUMMARY 항목 목록을 지운다. ClearSummaryItems(); // 통화 컬럼과 값 컬럼 목록에 SUMMARY 항목을 추가한다. AddSummaryItems(); //////////////////////////////////////////////////////////////////////////////////////////////////// this.gridControl.DataSource = sampleTable; this.gridView.BestFitColumns(); } #endregion #region 그리드 뷰 커스텀 SUMMARY 계산시 처리하기 - gridView_CustomSummaryCalculate(sender, e) /// <summary> /// 그리드 뷰 커스텀 SUMMARY 계산시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void gridView_CustomSummaryCalculate(object sender, CustomSummaryEventArgs e) { if(e.SummaryProcess == CustomSummaryProcess.Start) { if(this.fieldSummarySortedList == null) { this.fieldSummarySortedList = new SortedList<string, SortedList<string, decimal>>(); } else { for(int i = 0; i < this.fieldSummarySortedList.Keys.Count; i++) { SortedList<string, decimal> slCurrencySummary = this.fieldSummarySortedList[this.fieldSummarySortedList.Keys[i]]; slCurrencySummary.Clear(); } this.fieldSummarySortedList.Clear(); } for(int i = 0; i < sourceValueColumnArray.Length; i++) { SortedList<string, decimal> slCurrencySummary = new SortedList<string, decimal>(); for(int j = 0; j < this.currencySortedList.Keys.Count; j++) { string currency = this.currencySortedList.Keys[j]; slCurrencySummary.Add(currency, 0m); } this.fieldSummarySortedList.Add(sourceValueColumnArray[i].FieldName, slCurrencySummary); } } if(e.SummaryProcess == CustomSummaryProcess.Calculate) { if(e.FieldValue != null && e.FieldValue != DBNull.Value) { DataRow row = this.gridView.GetDataRow(e.RowHandle); string currency; if(row[sourceCurrencyColumn.FieldName] == DBNull.Value || row[sourceCurrencyColumn.FieldName] == null) { currency = string.Empty; } else { currency = row[sourceCurrencyColumn.FieldName] as string; } for(int i = 0; i < sourceValueColumnArray.Length; i++) { decimal value; if(row[sourceValueColumnArray[i].FieldName] == DBNull.Value || row[sourceValueColumnArray[i].FieldName] == null) { value = 0m; } else { value = (decimal)row[sourceValueColumnArray[i].FieldName]; } SortedList<string, decimal> currencySummarySortedList = this.fieldSummarySortedList[sourceValueColumnArray[i].FieldName]; if(currencySummarySortedList.ContainsKey(currency)) { currencySummarySortedList[currency] += value; } } } } string tag = (e.Item as GridSummaryItem).Tag as string; if(e.SummaryProcess == CustomSummaryProcess.Finalize) { if(string.IsNullOrEmpty(tag)) { return; } string[] tagValueArray = tag.Split('_'); string fieldName = tagValueArray[0]; string currency = tagValueArray[1]; SortedList<string, decimal> currencySummarySortedList = this.fieldSummarySortedList[fieldName]; if(currencySummarySortedList.ContainsKey(currency)) { e.TotalValue = currencySummarySortedList[currency]; } } } #endregion //////////////////////////////////////////////////////////////////////////////// Function #region 샘플 리스트 구하기 - GetSampleList() /// <summary> /// 샘플 리스트 구하기 /// </summary> /// <returns>샘플 리스트</returns> private DataTable GetSampleList() { DataTable table = new DataTable(); table.Columns.Add("Currency", typeof(string )); table.Columns.Add("Amount1" , typeof(decimal)); table.Columns.Add("Amount2" , typeof(decimal)); DataRow row = null; row = table.NewRow(); row["Currency"] = "KRW"; row["Amount1" ] = 10000m; row["Amount2" ] = 13000m; table.Rows.Add(row); row = table.NewRow(); row["Currency"] = "USD"; row["Amount1" ] = 2000m; row["Amount2" ] = 12000m; table.Rows.Add(row); row = table.NewRow(); row["Currency"] = "KRW"; row["Amount1" ] = 13000m; row["Amount2" ] = 8000m; table.Rows.Add(row); row = table.NewRow(); row["Currency"] = "KRW"; row["Amount1" ] = 8000m; row["Amount2" ] = 4000m; table.Rows.Add(row); row = table.NewRow(); row["Currency"] = "USD"; row["Amount1" ] = 5000m; row["Amount2" ] = 4000m; table.Rows.Add(row); row = table.NewRow(); row["Currency"] = "JPN"; row["Amount1" ] = 15000m; row["Amount2" ] = 11000m; table.Rows.Add(row); return table; } #endregion #region 통화 정렬 리스트 구하기 - GetCurrencySortedList(sourceTable) /// <summary> /// 통화 정렬 리스트 구하기 /// </summary> /// <param name="sourceTable">소스 테이블</param> /// <returns>통화 정렬 리스트</returns> private SortedList<string, string> GetCurrencySortedList(DataTable sourceTable) { SortedList<string, string> currencySortedList = new SortedList<string, string>(); if(sourceTable == null || sourceTable.Rows.Count == 0) { return currencySortedList; } foreach(DataRow sourceRow in sourceTable.Rows) { string currency; if(sourceRow[sourceCurrencyColumn.FieldName] == DBNull.Value || sourceRow[sourceCurrencyColumn.FieldName] == null) { currency = string.Empty; } else { currency = sourceRow[sourceCurrencyColumn.FieldName] as string; } if(!currencySortedList.ContainsKey(currency)) { currencySortedList.Add(currency, currency); } } return currencySortedList; } #endregion #region SUMMARY 항목들 지우기 - ClearSummaryItems() /// <summary> /// SUMMARY 항목들 지우기 /// </summary> private void ClearSummaryItems() { // 통화 컬럼의 SUMMARY 항목 목록을 지운다. for(int i = this.sourceCurrencyColumn.Summary.Count - 1; i > -1; i--) { if(this.sourceCurrencyColumn.Summary[i].SummaryType == SummaryItemType.Custom) { this.sourceCurrencyColumn.Summary.RemoveAt(i); } } // 값 컬럼 목록의 SUMMARY 항목 목록을 지운다. for(int i = 0; i < this.sourceValueColumnArray.Length; i++) { for(int j = this.sourceValueColumnArray[i].Summary.Count - 1; j > -1; j--) { if(this.sourceValueColumnArray[i].Summary[j].SummaryType == SummaryItemType.Custom) { this.sourceValueColumnArray[i].Summary.RemoveAt(j); } } } } #endregion #region SUMMARY 항목들 추가하기 - AddSummaryItems() /// <summary> /// SUMMARY 항목들 추가하기 /// </summary> private void AddSummaryItems() { GridColumnSummaryItem gridColumnSummaryItem = null; for(int i = 0; i < this.currencySortedList.Keys.Count; i++) { string currency = this.currencySortedList.Keys[i]; gridColumnSummaryItem = new GridColumnSummaryItem(); gridColumnSummaryItem.DisplayFormat = currency; gridColumnSummaryItem.FieldName = sourceCurrencyColumn.FieldName; gridColumnSummaryItem.SummaryType = SummaryItemType.Count; sourceCurrencyColumn.Summary.Add(gridColumnSummaryItem); for(int j = 0; j < sourceValueColumnArray.Length; j++) { GridColumn valueColumn = sourceValueColumnArray[j]; gridColumnSummaryItem = new GridColumnSummaryItem(); gridColumnSummaryItem.DisplayFormat = "{0:N2}"; gridColumnSummaryItem.FieldName = valueColumn.FieldName; gridColumnSummaryItem.SummaryType = SummaryItemType.Custom; gridColumnSummaryItem.Tag = valueColumn.FieldName + "_" + currency; valueColumn.Summary.Add(gridColumnSummaryItem); } } } #endregion } } |