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

728x90
반응형
728x170

TestProject.zip
다운로드

▶ MainForm.cs

using System;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Windows.Forms;

using DevExpress.Data;
using DevExpress.LookAndFeel;
using DevExpress.Utils;
using DevExpress.XtraEditors;
using DevExpress.XtraGrid;
using DevExpress.XtraGrid.Views.BandedGrid;
using DevExpress.XtraGrid.Views.Base;

namespace TestProject
{
    /// <summary>
    /// 메인 폼
    /// </summary>
    public partial class MainForm : XtraForm
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Field
        ////////////////////////////////////////////////////////////////////////////////////////// Private

        #region Field

        /// <summary>
        /// 디폴트 룩앤필
        /// </summary>
        private DefaultLookAndFeel defaultLookAndFeel = null;

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor
        ////////////////////////////////////////////////////////////////////////////////////////// Public

        #region 생성자 - MainForm()

        /// <summary>
        /// 생성자
        /// </summary>
        public MainForm()
        {
            InitializeComponent();
        }

        #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)
        {
            this.defaultLookAndFeel = new DefaultLookAndFeel();

            GridControl gridControl = GetGridControl("gridControl");

            gridControl.Dock = DockStyle.Fill;

            BeginInitialize(gridControl);

            AdvBandedGridView advBandedGridView = GetAdvancedBandedGridView("schoolAdvBandedGridView", false, true, false, false);

            advBandedGridView.Appearance.BandPanel.Font   = new Font("나눔고딕코딩", 12f);
            advBandedGridView.Appearance.Row.Font         = new Font("나눔고딕코딩", 12f);
            advBandedGridView.Appearance.HeaderPanel.Font = new Font("나눔고딕코딩", 12f);

            AddView(gridControl, advBandedGridView, true);

            BeginInitialize(advBandedGridView);

            GridBand studentGridBand     = GetGridBand("Student"    , 200, "학생");
            GridBand informationGridBand = GetGridBand("Information", 300, "정보");
                GridBand bodyGridBand    = GetGridBand("Body"       , 200, "신체");
                GridBand otherGridBand   = GetGridBand("Other"      , 100, "기타");
            
            AddBand(advBandedGridView, studentGridBand    );
            AddBand(advBandedGridView, informationGridBand);
                AddChildGridBand(informationGridBand, bodyGridBand);
                AddChildGridBand(informationGridBand, otherGridBand);

            AddGridColumn
            (
                advBandedGridView,
                studentGridBand,
                GetBandedGridColumn("학번", "학번", UnboundColumnType.String, 100, HorzAlignment.Center, false, FormatType.None, null)
            );
            AddGridColumn
            (
                advBandedGridView,
                studentGridBand,
                GetBandedGridColumn("성명", "성명", UnboundColumnType.String, 100, HorzAlignment.Center, false, FormatType.None, null)
            );

            AddGridColumn
            (
                advBandedGridView,
                bodyGridBand,
                GetBandedGridColumn("신장", "신장", UnboundColumnType.Object, 100, HorzAlignment.Far, false, FormatType.Numeric, "#,##0.00")
            );
            AddGridColumn
            (
                advBandedGridView,
                bodyGridBand,
                GetBandedGridColumn("체중", "체중", UnboundColumnType.Object, 100, HorzAlignment.Far, false, FormatType.Numeric, "#,##0.00")
            );

            AddGridColumn
            (
                advBandedGridView,
                otherGridBand,
                GetBandedGridColumn("추가", "추가", UnboundColumnType.Boolean, 100, HorzAlignment.Center, false, FormatType.None, null)
            );

            EndInitialize(advBandedGridView);
            
            EndInitialize(gridControl);

            Controls.Add(gridControl);

            DataTable table = GetDataTable();

            SetDataTable(table);

            gridControl.DataSource = table;
        }

        #endregion

        //////////////////////////////////////////////////////////////////////////////// Function

        #region 그리드 컨트롤 구하기 - GetGridControl(name)

        /// <summary>
        /// 그리드 컨트롤 구하기
        /// </summary>
        /// <param name="name">명칭</param>
        /// <returns>그리드 컨트롤</returns>
        private GridControl GetGridControl(string name)
        {
            GridControl gridControl = new GridControl();

            gridControl.Name = name;

            return gridControl;
        }

        #endregion
        #region 초기화 시작하기 - BeginInitialize(gridControl)

        /// <summary>
        /// 초기화 시작하기
        /// </summary>
        /// <param name="gridControl">그리드 컨트롤</param>
        private void BeginInitialize(GridControl gridControl)
        {
            ((ISupportInitialize)gridControl).BeginInit();
        }

        #endregion
        #region 초기화 종료하기 - EndInitialize(gridControl)

        /// <summary>
        /// 초기화 종료하기
        /// </summary>
        /// <param name="gridControl">그리드 컨트롤</param>
        private void EndInitialize(GridControl gridControl)
        {
            ((ISupportInitialize)gridControl).EndInit();
        }

        #endregion
        #region 뷰 추가하기 - AddView(gridControl, baseView, isMainView)

        /// <summary>
        /// 뷰 추가하기
        /// </summary>
        /// <param name="gridControl">그리드 컨트롤</param>
        /// <param name="baseView">기본 뷰</param>
        /// <param name="isMainView">메인 뷰 여부</param>
        private void AddView(GridControl gridControl, BaseView baseView, bool isMainView)
        {
            gridControl.ViewCollection.Add(baseView);

            if(isMainView)
            {
                gridControl.MainView = baseView;
            }

            baseView.GridControl = gridControl;
        }

        #endregion

        #region 개선 밴드 그리드 뷰 구하기 - GetAdvancedBandedGridView(name, editable, multiSelect, showGroupPanel, showIndicator)

        /// <summary>
        /// 개선 밴드 그리드 뷰 구하기
        /// </summary>
        /// <param name="name">명칭</param>
        /// <param name="editable">편집 가능 여부</param>
        /// <param name="multiSelect">복수 선택 여부</param>
        /// <param name="showGroupPanel">그룹 패널 표시 여부</param>
        /// <param name="showIndicator">지시자 표시 여부</param>
        /// <returns>개선 밴드 그리드 뷰</returns>
        private AdvBandedGridView GetAdvancedBandedGridView(string name, bool editable, bool multiSelect, bool showGroupPanel,
            bool showIndicator)
        {
            AdvBandedGridView advBandedGridView = new AdvBandedGridView();

            advBandedGridView.Name                                     = name;
            advBandedGridView.OptionsBehavior.Editable                 = editable;
            advBandedGridView.OptionsBehavior.EditorShowMode           = EditorShowMode.MouseDown;
            advBandedGridView.OptionsCustomization.AllowColumnMoving   = false;
            advBandedGridView.OptionsCustomization.AllowColumnResizing = false;
            advBandedGridView.OptionsCustomization.AllowFilter         = false;
            advBandedGridView.OptionsCustomization.AllowSort           = false;
            advBandedGridView.OptionsMenu.EnableColumnMenu             = false;
            advBandedGridView.OptionsSelection.MultiSelect             = multiSelect;
            advBandedGridView.OptionsView.ColumnAutoWidth              = false;
            advBandedGridView.OptionsView.ShowGroupPanel               = showGroupPanel;
            advBandedGridView.OptionsView.ShowIndicator                = showIndicator;

            return advBandedGridView;
        }

        #endregion
        #region 초기화 시작하기 - BeginInitialize(advBandedGridView)

        /// <summary>
        /// 초기화 시작하기
        /// </summary>
        /// <param name="advBandedGridView">개선 밴드 그리드 뷰</param>
        private void BeginInitialize(AdvBandedGridView advBandedGridView)
        {
            ((ISupportInitialize)advBandedGridView).BeginInit();
        }

        #endregion
        #region 초기화 종료하기 - EndInitialize(advBandedGridView)

        /// <summary>
        /// 초기화 종료하기
        /// </summary>
        /// <param name="advBandedGridView">개선 밴드 그리드 뷰</param>
        private void EndInitialize(AdvBandedGridView advBandedGridView)
        {
            ((ISupportInitialize)advBandedGridView).EndInit();
        }

        #endregion
        #region 밴드 추가하기 - AddBand(advBandedGridView, gridBand)

        /// <summary>
        /// 밴드 추가하기
        /// </summary>
        /// <param name="advBandedGridView">개선 밴드 그리드 뷰</param>
        /// <param name="gridBand">그리드 밴드</param>
        private void AddBand(AdvBandedGridView advBandedGridView, GridBand gridBand)
        {
            advBandedGridView.Bands.Add(gridBand);
        }

        #endregion

        #region 그리드 밴드 구하기 - GetGridBand(name, width, caption, horzAlignment, vertAlignment)

        /// <summary>
        /// 그리드 밴드 구하기
        /// </summary>
        /// <param name="name">명칭</param>
        /// <param name="width">너비</param>
        /// <param name="caption">제목</param>
        /// <param name="horzAlignment">수평 정렬</param>
        /// <param name="vertAlignment">수직 정렬</param>
        /// <returns>그리드 밴드</returns>
        private GridBand GetGridBand(string name, int width, string caption, HorzAlignment horzAlignment, VertAlignment vertAlignment)
        {
            GridBand gridBand = new GridBand();

            gridBand.Name                                    = name;
            gridBand.Width                                   = width;
            gridBand.Caption                                 = caption;
            gridBand.AppearanceHeader.Options.UseTextOptions = true;
            gridBand.AppearanceHeader.TextOptions.HAlignment = horzAlignment;
            gridBand.AppearanceHeader.TextOptions.VAlignment = vertAlignment;

            return gridBand;
        }

        #endregion
        #region 그리드 밴드 구하기 - GetGridBand(name, width, caption)

        /// <summary>
        /// 그리드 밴드 구하기
        /// </summary>
        /// <param name="name">명칭</param>
        /// <param name="width">너비</param>
        /// <param name="caption">제목</param>
        /// <returns>그리드 밴드</returns>
        private GridBand GetGridBand(string name, int width, string caption)
        {
            return GetGridBand(name, width, caption, HorzAlignment.Center, VertAlignment.Center);
        }

        #endregion
        #region 자식 그리드 밴드 추가하기 - AddChildGridBand(parentGridBand, childGridBand)

        /// <summary>
        /// 자식 그리드 밴드 추가하기
        /// </summary>
        /// <param name="parentGridBand">부모 그리드 밴드</param>
        /// <param name="childGridBand">자식 그리드 밴드</param>
        private void AddChildGridBand(GridBand parentGridBand, GridBand childGridBand)
        {
            parentGridBand.Children.Add(childGridBand);
        }

        #endregion
        #region 밴드 그리드 컬럼 구하기 - GetBandedGridColumn(caption, fieldName, unboundColumnType, width, horzAlignment,
           allowEdit, formatType, formatString)

        /// <summary>
        /// 밴드 그리드 컬럼 구하기
        /// </summary>
        /// <param name="caption">제목</param>
        /// <param name="fieldName">필드명</param>
        /// <param name="unboundColumnType">언바운드 컬럼 종류</param>
        /// <param name="width">너비</param>
        /// <param name="horzAlignment">수평 정렬</param>
        /// <param name="allowEdit">편집 허용 여부</param>
        /// <param name="formatType">형식 종류</param>
        /// <param name="formatString">형식 문자열</param>
        /// <returns>밴드 그리드 컬럼</returns>
        private BandedGridColumn GetBandedGridColumn(string caption, string fieldName, UnboundColumnType unboundColumnType, int width,
            HorzAlignment horzAlignment, bool allowEdit, FormatType formatType, string formatString)
        {
            BandedGridColumn bandedGridColumn = new BandedGridColumn();

            bandedGridColumn.Name                                    = string.Empty;
            bandedGridColumn.Caption                                 = caption;
            bandedGridColumn.FieldName                               = fieldName;
            bandedGridColumn.UnboundType                             = unboundColumnType;
            bandedGridColumn.Width                                   = width;
            bandedGridColumn.AutoFillDown                            = true;
            bandedGridColumn.AppearanceHeader.Options.UseTextOptions = true;
            bandedGridColumn.AppearanceHeader.TextOptions.HAlignment = HorzAlignment.Center;
            bandedGridColumn.AppearanceCell.Options.UseTextOptions   = true;
            bandedGridColumn.AppearanceCell.TextOptions.HAlignment   = horzAlignment;
            bandedGridColumn.OptionsColumn.AllowEdit                 = allowEdit;
            bandedGridColumn.DisplayFormat.FormatType                = formatType;
            bandedGridColumn.DisplayFormat.FormatString              = formatString;
            bandedGridColumn.Visible                                 = true;

            return bandedGridColumn;
        }

        #endregion
        #region 그리드 컬럼 추가하기 - AddGridColumn(advBandedGridView, gridBand, bandedGridColumn)

        /// <summary>
        /// 그리드 컬럼 추가하기
        /// </summary>
        /// <param name="advBandedGridView">개선 밴드 그리드 뷰</param>
        /// <param name="gridBand">그리드 밴드</param>
        /// <param name="bandedGridColumn">밴드 그리드 컬럼</param>
        private void AddGridColumn(AdvBandedGridView advBandedGridView, GridBand gridBand, BandedGridColumn bandedGridColumn)
        {
            advBandedGridView.Columns.Add(bandedGridColumn);

            if(gridBand != null)
            {
                gridBand.Columns.Add(bandedGridColumn);
            }
        }

        #endregion

        #region 데이타 테이블 구하기 - GetDataTable()

        /// <summary>
        /// 데이타 테이블 구하기
        /// </summary>
        /// <returns>데이타 테이블</returns>
        public DataTable GetDataTable()
        {
            DataTable table = new DataTable();

            table.Columns.Add(new DataColumn("학번", typeof(string)));
            table.Columns.Add(new DataColumn("성명", typeof(string)));
            table.Columns.Add(new DataColumn("신장", typeof(double)));
            table.Columns.Add(new DataColumn("체중", typeof(double)));
            table.Columns.Add(new DataColumn("비고", typeof(string)));
            table.Columns.Add(new DataColumn("추가", typeof(bool  )));
            
            return table;
        }

        #endregion
        #region 데이타 테이블 설정하기 - SetDataTable(sourceTable)

        /// <summary>
        /// 데이타 테이블 설정하기
        /// </summary>
        /// <param name="sourceTable">소스 테이블</param>
        public void SetDataTable(DataTable sourceTable)
        {
            DataRow row = null;
            
            row = sourceTable.NewRow();
            
            row["학번"] = "2000-0001";
            row["성명"] = "홍길동";
            row["신장"] = 174d;
            row["체중"] = 83d;
            row["비고"] = "테스트1";
            row["추가"] = true;

            sourceTable.Rows.Add(row);

            row = sourceTable.NewRow();
            
            row["학번"] = "2000-0002";
            row["성명"] = "김미숙";
            row["신장"] = 164d;
            row["체중"] = 46d;
            row["비고"] = "테스트2";
            row["추가"] = true;

            sourceTable.Rows.Add(row);

            row = sourceTable.NewRow();
            
            row["학번"] = "2000-0003";
            row["성명"] = "홍길숙";
            row["신장"] = 171d;
            row["체중"] = 74d;
            row["비고"] = "테스트3";
            row["추가"] = false;

            sourceTable.Rows.Add(row);
        }

        #endregion
    }
}
728x90
반응형
그리드형
Posted by 사용자 icodebroker

댓글을 달아 주세요