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

728x90
반응형
728x170

TestProject.zip
다운로드

▶ MainForm.cs

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

using DevExpress.Utils.Drawing;
using DevExpress.XtraEditors.Repository;
using DevExpress.XtraEditors.Drawing;
using DevExpress.XtraEditors.ViewInfo;
using DevExpress.XtraGrid.Columns;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraGrid.Views.Grid.ViewInfo;

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

        #region Field

        /// <summary>
        /// 저장소 항목 체크 에디터
        /// </summary>
        private RepositoryItemCheckEdit repositoryItemCheckEdit = new RepositoryItemCheckEdit();

        #endregion

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

        #region 생성자 - MainForm()

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

            this.gridView.OptionsView.ShowGroupPanel = false;

            BindingSource bindingSource = new BindingSource();

            bindingSource.DataSource = GetDataTable(5);

            this.gridControl.DataSource = bindingSource;


            this.gridView.CustomDrawColumnHeader += gridView_CustomDrawColumnHeader;
            this.gridView.MouseDown              += gridView_MouseDown;
        }

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Method
        ////////////////////////////////////////////////////////////////////////////////////////// Private
        //////////////////////////////////////////////////////////////////////////////// Event

        #region 그리드 뷰 컬럼 헤더 커스텀 그리기 - gridView_CustomDrawColumnHeader(sender, e)

        /// <summary>
        /// 그리드 뷰 컬럼 헤더 커스텀 그리기
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private void gridView_CustomDrawColumnHeader(object sender, ColumnHeaderCustomDrawEventArgs e)
        {
            GridView gridView = sender as GridView;

            if(e.Column == gridView.Columns["Select"])
            {
                e.Info.InnerElements.Clear();

                e.Info.Appearance.ForeColor = Color.Blue;

                e.Painter.DrawObject(e.Info);

                DrawCheckBox(e.Graphics, e.Bounds, GetCheckCount() == gridView.DataRowCount);

                e.Handled = true;
            }
        }

        #endregion
        #region 그리드 뷰 마우스 DOWN 처리하기 - gridView_MouseDown(sender, e)

        /// <summary>
        /// 그리드 뷰 마우스 DOWN 처리하기
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private void gridView_MouseDown(object sender, MouseEventArgs e)
        {
            GridView gridView = sender as GridView;

            if(e.Clicks == 1 && e.Button == MouseButtons.Left)
            {
                Point mousePoint = gridView.GridControl.PointToClient(Control.MousePosition);

                GridHitInfo gridHitInfo = gridView.CalcHitInfo(mousePoint);

                if(gridHitInfo.InColumn && gridHitInfo.Column.FieldName == "Select")
                {
                    if(GetCheckCount() == gridView.DataRowCount)
                    {
                        UnchekAll();
                    }
                    else
                    {
                        CheckAll();
                    }
                }
            }
        }

        #endregion

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

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

        /// <summary>
        /// 데이터 테이블 구하기
        /// </summary>
        /// <param name="rowCount">행 수</param>
        /// <returns>데이터 테이블</returns>
        private static DataTable GetDataTable(int rowCount)
        {
            DataTable table = new DataTable();

            table.Columns.Add("Name"  , typeof(string  ));
            table.Columns.Add("ID"    , typeof(int     ));
            table.Columns.Add("Number", typeof(int     ));
            table.Columns.Add("Date"  , typeof(DateTime));
            table.Columns.Add("Select", typeof(bool    ));

            for(int i = 0; i < rowCount; i++)
            {
                table.Rows.Add(new object[] { string.Format("성명 {0}", i), i, 3 - i, DateTime.Now.AddDays(i), false });
            }

            return table;
        }

        #endregion

        #region 체크 박스 그리기 - DrawCheckBox(graphics, rectangle, isChecked)

        /// <summary>
        /// 체크 박스 그리기
        /// </summary>
        /// <param name="graphics">그래픽스</param>
        /// <param name="rectangle">사각형</param>
        /// <param name="isChecked">체크 여부</param>
        protected void DrawCheckBox(Graphics graphics, Rectangle rectangle, bool isChecked)
        {
            CheckEditViewInfo checkEditViewInfo = this.repositoryItemCheckEdit.CreateViewInfo() as CheckEditViewInfo;

            checkEditViewInfo.Bounds                    = rectangle;
            checkEditViewInfo.PaintAppearance.ForeColor = Color.Black;
            checkEditViewInfo.EditValue                 = isChecked;

            checkEditViewInfo.CalcViewInfo(graphics);

            CheckEditPainter checkEditPainter = this.repositoryItemCheckEdit.CreatePainter() as CheckEditPainter;

            ControlGraphicsInfoArgs e = new ControlGraphicsInfoArgs(checkEditViewInfo, new GraphicsCache(graphics), rectangle);

            checkEditPainter.Draw(e);

            e.Cache.Dispose();
        }

        #endregion
        #region 체크 카운트 구하기 - GetCheckCount()

        /// <summary>
        /// 체크 카운트 구하기
        /// </summary>
        /// <returns></returns>
        private int GetCheckCount()
        {
            int count = 0;

            GridColumn selectColumn = this.gridView.Columns["Select"];

            for(int i = 0; i < this.gridView.DataRowCount; i++)
            {
                if((bool)this.gridView.GetRowCellValue(i, selectColumn) == true)
                {
                    count++;
                }
            }

            return count;
        }

        #endregion
        #region 모두 체크하기 - CheckAll()

        /// <summary>
        /// 모두 체크하기
        /// </summary>
        private void CheckAll() 
        {
            GridColumn selectColumn = this.gridView.Columns["Select"];

            for(int i = 0; i < this.gridView.DataRowCount; i++)
            {
                this.gridView.SetRowCellValue(i, selectColumn, true);
            }
        }

        #endregion
        #region 모두 체크 취소하기 - UnchekAll()

        /// <summary>
        /// 모두 체크 취소하기
        /// </summary>
        private void UnchekAll() 
        {
            GridColumn selectColumn = this.gridView.Columns["Select"];

            for(int i = 0; i < this.gridView.DataRowCount; i++)
            {
                this.gridView.SetRowCellValue(i, selectColumn, false);
            }
        }

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

댓글을 달아 주세요