첨부 소스 코드는 나눔고딕코딩 폰트를 사용합니다.
728x90
반응형
728x170

TestProject.zip
다운로드

▶ FocusRowHelper.cs

using System.Windows.Forms;

using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraGrid.Views.Grid.ViewInfo;

namespace TestProject
{
    /// <summary>
    /// 포커스 행 헬퍼
    /// </summary>
    public class FocusRowHelper
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Field
        ////////////////////////////////////////////////////////////////////////////////////////// Private

        #region Field

        /// <summary>
        /// 그리드 뷰
        /// </summary>
        private GridView gridView = null;

        /// <summary>
        /// 이전 포커스 사각형 그리기 스타일
        /// </summary>
        private DrawFocusRectStyle previousDrawFocusRectStyle;

        /// <summary>
        /// 포커스 행 하이라이트 여부
        /// </summary>
        private bool focusRowVisible = true;

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Property
        ////////////////////////////////////////////////////////////////////////////////////////// Public

        #region 첫번째 클릭시 포커스 행 표시 여부 - ShowFocusRowOnFirstClick

        /// <summary>
        /// 첫번째 클릭시 포커스 행 표시 여부
        /// </summary>
        public bool ShowFocusRowOnFirstClick { get; set; }

        #endregion

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

        #region 생성자 - FocusRowHelper(gridView)

        /// <summary>
        /// 생성자
        /// </summary>
        /// <param name="gridView">그리드 뷰</param>
        public FocusRowHelper(GridView gridView)
        {
            this.gridView = gridView;

            ShowFocusRowOnFirstClick = true;

            Activate();

            SetFocusRowVisible(false);
        }

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Method
        ////////////////////////////////////////////////////////////////////////////////////////// Public

        #region 활성화 시키기 - Activate()

        /// <summary>
        /// 활성화 시키기
        /// </summary>
        public void Activate()
        {
            this.gridView.CustomDrawRowIndicator += GridView_CustomDrawRowIndicator;
            this.gridView.MouseDown              += GridView_MouseDown;
        }

        #endregion
        #region 비활성화 시키기 - Deactivate()

        /// <summary>
        /// 비활성화 시키기
        /// </summary>
        public void Deactivate()
        {
            SetFocusRowVisible(true);

            this.gridView.CustomDrawRowIndicator -= GridView_CustomDrawRowIndicator;
            this.gridView.MouseDown              -= GridView_MouseDown;
        }

        #endregion

        #region 포커스 행 표시 여부 설정하기 - SetFocusRowVisible(visible)

        /// <summary>
        /// 포커스 행 표시 여부 설정하기
        /// </summary>
        /// <param name="visible">표시 여부</param>
        public void SetFocusRowVisible(bool visible)
        {
            this.focusRowVisible = visible;

            if(!visible)
            {
                this.previousDrawFocusRectStyle = this.gridView.FocusRectStyle;
            }

            this.gridView.BeginUpdate();

            this.gridView.FocusRectStyle = visible ? this.previousDrawFocusRectStyle : DrawFocusRectStyle.None;

            this.gridView.OptionsSelection.EnableAppearanceFocusedCell   = this.focusRowVisible;
            this.gridView.OptionsSelection.EnableAppearanceFocusedRow    = this.focusRowVisible;
            this.gridView.OptionsSelection.EnableAppearanceHideSelection = this.focusRowVisible;

            this.gridView.EndUpdate();
        }

        #endregion

        ////////////////////////////////////////////////////////////////////////////////////////// Private
        //////////////////////////////////////////////////////////////////////////////// Event

        #region 그리드 뷰 행 지시자 커스텀 그리기 - GridView_CustomDrawRowIndicator(sender, e)

        /// <summary>
        /// 그리드 뷰 행 지시자 커스텀 그리기
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private void GridView_CustomDrawRowIndicator(object sender, RowIndicatorCustomDrawEventArgs e)
        {
            if(!this.focusRowVisible)
            {
                e.Info.ImageIndex = -1;
            }
        }

        #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)
        {
            if(ShowFocusRowOnFirstClick)
            {
                GridHitInfo gridHitInfo = this.gridView.CalcHitInfo(e.Location);

                if(gridHitInfo.InRow)
                {
                    SetFocusRowVisible(true);
                }
            }
        }

        #endregion
    }
}

 

728x90

 

▶ MainForm.cs

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

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

        #region Field

        /// <summary>
        /// 헬퍼
        /// </summary>
        private FocusRowHelper helper;

        #endregion

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

        #region 생성자 - MainForm()

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

            this.gridView.OptionsBehavior.Editable = false;

            this.helper = new FocusRowHelper(this.gridView);

            this.Load += Form_Load;
        }

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Method
        ////////////////////////////////////////////////////////////////////////////////////////// Protected

        #region 폼 닫을 경우 처리하기 - OnFormClosing(e)

        /// <summary>
        /// 폼 닫을 경우 처리하기
        /// </summary>
        /// <param name="e">이벤트 인자</param>
        protected override void OnFormClosing(FormClosingEventArgs e)
        {
            base.OnFormClosing(e);

            this.helper.Deactivate();
        }

        #endregion

        ////////////////////////////////////////////////////////////////////////////////////////// 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.gridControl.DataSource = GetSampleData();
        }

        #endregion

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

        #region 샘플 데이터 구하기 - GetSampleData()

        /// <summary>
        /// 샘플 데이터 구하기
        /// </summary>
        /// <returns>샘플 데이터</returns>
        private DataTable GetSampleData()
        {
            DataTable table = new DataTable();

            table.Columns.Add("ID"  , typeof(int   ));
            table.Columns.Add("Name", typeof(string));

            for(int i = 1; i <= 100; i++)
            {
                table.Rows.Add(new object[] { i, "항목 " + i.ToString() });
            }

            table.AcceptChanges();

            return table;
        }

        #endregion
    }
}
728x90
반응형
그리드형(광고전용)
Posted by icodebroker

댓글을 달아 주세요