ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • C# DEVEXPRESS GridView header 컬럼 체크 박스 사용 (All checked)
    개발 공부/C# 2022. 11. 22. 16:21
    반응형
    SMALL

    데브 grid control 사용시 헤더에 체크박스 넣어서 전체선택/해제 하는게 없음

    따로 구현해줘야함

     

        
        /// <summary>
        /// 그리드 컬럼 체커
        /// </summary>
        public class GridColumnChecker
        {
            //////////////////////////////////////////////////////////////////////////////////////////////////// Field
            ////////////////////////////////////////////////////////////////////////////////////////// Private
    
            #region Field
    
            /// <summary>
            /// 마진
            /// </summary>
            private const int MARGIN = 4;
    
            /// <summary>
            /// 그리드 뷰
            /// </summary>
            private GridView gridView;
    
            /// <summary>
            /// 그리드 컬럼
            /// </summary>
            private GridColumn gridColumn;
    
            /// <summary>
            /// 체크 에디트
            /// </summary>
            private CheckEdit checkEdit;
    
            /// <summary>
            /// 체크 이벤트 발생 여부
            /// </summary>
            private bool firingCheckEvent = false;
    
            #endregion
    
            //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor
            ////////////////////////////////////////////////////////////////////////////////////////// Public
    
            #region 생성자 - GridColumnChecker(gridView, gridColumn)
    
            /// <summary>
            /// 생성자
            /// </summary>
            /// <param name="gridView">그리드 뷰</param>
            /// <param name="gridColumn">그리드 컬럼</param>
            public GridColumnChecker(GridView gridView, GridColumn gridColumn)
            {
                this.gridView = gridView;
    
                this.gridColumn = gridColumn;
    
                this.checkEdit = new CheckEdit();
    
                this.checkEdit.Text = string.Empty;
                this.checkEdit.Width = 19;
                this.checkEdit.Height = 19;
    
                this.checkEdit.EditValueChanged += this.checkEdit_EditValueChanged;
    
                this.gridView.GridControl.Controls.Add(this.checkEdit);
    
                this.firingCheckEvent = true;
    
                this.checkEdit.EditValue = IsAllSelected();
    
                this.firingCheckEvent = false;
    
                this.gridView.CellValueChanged += this.gridView_CellValueChanged;
                this.gridView.RowCellClick += this.gridView_RowCellClick;
                this.gridView.CustomDrawColumnHeader += this.gridView_CustomDrawColumnHeader;
            }
    
            #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)
            {
                if (e.Column == this.gridColumn)
                {
                    e.Info.InnerElements.Clear();
    
                    e.Painter.DrawObject(e.Info);
    
                    this.checkEdit.Location = new Point(e.Bounds.Width - this.checkEdit.Width - MARGIN + e.Bounds.X, (e.Bounds.Height - this.checkEdit.Height) / 2 + e.Bounds.Y);
    
                    e.Handled = true;
                }
            }
    
            #endregion
            #region 그리드 뷰 행/셀 클릭시 처리하기 - gridView_RowCellClick(sender, e)
    
            /// <summary>
            /// 그리드 뷰 행/셀 클릭시 처리하기
            /// </summary>
            /// <param name="sender">이벤트 발생자</param>
            /// <param name="e">이벤트 인자</param>
            private void gridView_RowCellClick(object sender, RowCellClickEventArgs e)
            {
                if (e.Column == this.gridColumn)
                {
                    InvertRowSelection(e.RowHandle);
    
                    this.firingCheckEvent = true;
    
                    this.checkEdit.EditValue = IsAllSelected();
    
                    this.firingCheckEvent = false;
                }
            }
    
            #endregion
            #region 그리드 뷰 셀 값 변경시 처리하기 - gridView_CellValueChanged(sender, e)
    
            /// <summary>
            /// 그리드 뷰 셀 값 변경시 처리하기
            /// </summary>
            /// <param name="sender">이벤트 발생자</param>
            /// <param name="e">이벤트 인자</param>
            private void gridView_CellValueChanged(object sender, CellValueChangedEventArgs e)
            {
                if (!this.firingCheckEvent)
                {
                    this.firingCheckEvent = true;
    
                    this.checkEdit.EditValue = IsAllSelected();
    
                    this.firingCheckEvent = false;
                }
            }
    
            #endregion
            #region 체크 에디트 편집 값 변경시 처리하기 - this._pCheckEdit_EditValueChanged(sender, e)
    
            /// <summary>
            /// 체크 에디트 편집 값 변경시 처리하기
            /// </summary>
            /// <param name="sender">이벤트 발생자</param>
            /// <param name="e">이벤트 인자</param>
            private void checkEdit_EditValueChanged(object sender, EventArgs e)
            {
                if (!this.firingCheckEvent)
                {
                    this.firingCheckEvent = true;
    
                    if (this.checkEdit.Checked)
                    {
                        SelectAll();
                    }
                    else
                    {
                        UnselectAll();
                    }
    
                    this.firingCheckEvent = false;
                }
            }
    
            #endregion
    
            //////////////////////////////////////////////////////////////////////////////// Function
    
            #region 행 선택 반전하기 - InvertRowSelection(rowHandle)
    
            /// <summary>
            /// 행 선택 반전하기
            /// </summary>
            /// <param name="rowHandle">행 핸들</param>
            private void InvertRowSelection(int rowHandle)
            {
                if (this.gridView.IsDataRow(rowHandle))
                {
                    bool isChecked = Convert.ToBoolean(this.gridView.GetRowCellValue(rowHandle, this.gridColumn));
    
                    this.gridView.SetRowCellValue(rowHandle, this.gridColumn, !isChecked);
                }
            }
    
            #endregion
            #region 전체 선택하기 - SelectAll()
    
            /// <summary>
            /// 전체 선택하기
            /// </summary>
            private void SelectAll()
            {
                for (int i = 0; i < this.gridView.DataRowCount; i++)
                {
                    this.gridView.SetRowCellValue(i, this.gridColumn, true);
                }
            }
    
            #endregion
            #region 전체 선택 취소하기 - UnselectAll()
    
            /// <summary>
            /// 전체 선택 취소하기
            /// </summary>
            private void UnselectAll()
            {
                for (int i = 0; i < this.gridView.DataRowCount; i++)
                {
                    this.gridView.SetRowCellValue(i, this.gridColumn, false);
                }
            }
    
            #endregion
            #region 전체 선택 여부 조사하기 - IsAllSelected()
    
            /// <summary>
            /// 전체 선택 여부 조사하기
            /// </summary>
            /// <returns>전체 선택 여부</returns>
            private bool IsAllSelected()
            {
                if (this.gridView.DataRowCount == 0)
                {
                    return false;
                }
    
                for (int i = 0; i < this.gridView.DataRowCount; i++)
                {
                    bool isChecked = Convert.ToBoolean(this.gridView.GetRowCellValue(i, this.gridColumn));
    
                    if (!isChecked)
                    {
                        return false;
                    }
                }
    
                return true;
            }
    
            #endregion
        }

     

     

    참고 사이트 https://icodebroker.tistory.com/4898#google_vignette

    반응형
    LIST

    댓글

Designed by Tistory.