첨부 실행 코드는 나눔고딕코딩 폰트를 사용합니다.
본 블로그는 광고를 포함하고 있습니다.
광고 클릭에서 발생하는 수익금은 모두 블로그 콘텐츠 향상을 위해 쓰여집니다.

728x90
반응형
728x170

TestProject.zip
다운로드

▶ MainForm.cs

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
    }
}
728x90
반응형
그리드형
Posted by 사용자 icodebroker

댓글을 달아 주세요