첨부 실행 코드는 나눔고딕코딩 폰트를 사용합니다.
유용한 소스 코드가 있으면 icodebroker@naver.com으로 보내주시면 감사합니다.
블로그 자료는 자유롭게 사용하세요.

■ GridView 클래스 : ShownEditor 이벤트를 사용해 내장 에디터 동적 사용하기

------------------------------------------------------------------------------------------------------------------------


TestProject.zip


MainForm.cs

 

 

using System;

 

using DevExpress.Utils;

using DevExpress.XtraEditors;

using DevExpress.XtraEditors.Controls;

using DevExpress.XtraEditors.Repository;

using DevExpress.XtraGrid.Columns;

using DevExpress.XtraGrid.Views.Base;

using DevExpress.XtraGrid.Views.Grid;

 

namespace TestProject

{

    /// <summary>

    /// 메인 폼

    /// </summary>

    public partial class MainForm : XtraForm

    {

        //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor

        ////////////////////////////////////////////////////////////////////////////////////////// Public

 

        #region 생성자 - MainForm()

 

        /// <summary>

        /// 생성자

        /// </summary>

        public MainForm()

        {

            InitializeComponent();

 

            #region 타입 저장소 항목 콤보 박스를 설정한다.

 

            RepositoryItemComboBox typeRepositoryItemComboBox = new RepositoryItemComboBox();

 

            typeRepositoryItemComboBox.TextEditStyle           = TextEditStyles.DisableTextEditor;

            typeRepositoryItemComboBox.AppearanceDropDown.Font = Font;

 

            typeRepositoryItemComboBox.Items.Add("타입 1");

            typeRepositoryItemComboBox.Items.Add("타입 2");

 

            #endregion

            #region 값 저장소 항목 콤보 박스를 설정한다.

 

            RepositoryItemComboBox valueRepositoryItemComboBox = new RepositoryItemComboBox();

 

            valueRepositoryItemComboBox.TextEditStyle           = TextEditStyles.DisableTextEditor;

            valueRepositoryItemComboBox.AppearanceDropDown.Font = Font;

 

            #endregion

            #region 그리드 뷰를 초기화 한다.

 

            this.gridView.OptionsDetail.EnableMasterViewMode = false;

            this.gridView.OptionsSelection.MultiSelect       = true;

            this.gridView.OptionsSelection.MultiSelectMode   = GridMultiSelectMode.CellSelect;

            this.gridView.FocusRectStyle                     = DrawFocusRectStyle.CellFocus;

 

            // 그룹 패널

            this.gridView.OptionsView.ShowGroupPanel = false;

 

            // 행

            this.gridView.IndicatorWidth                         = 30;

            this.gridView.OptionsView.ShowIndicator              = true;

            this.gridView.OptionsView.EnableAppearanceEvenRow    = false;

            this.gridView.OptionsView.EnableAppearanceOddRow     = false;

            this.gridView.OptionsBehavior.KeepFocusedRowOnUpdate = false;

            this.gridView.OptionsNavigation.AutoFocusNewRow      = true;

 

            // 컬럼

            this.gridView.OptionsBehavior.AutoPopulateColumns      = false;

            this.gridView.OptionsView.ShowColumnHeaders            = true;

            this.gridView.OptionsView.ColumnAutoWidth              = false;

            this.gridView.OptionsMenu.EnableColumnMenu             = false;

            this.gridView.OptionsNavigation.EnterMoveNextColumn    = true;

            this.gridView.OptionsCustomization.AllowColumnMoving   = true;

            this.gridView.OptionsCustomization.AllowColumnResizing = true;

            this.gridView.OptionsCustomization.AllowFilter         = false;

            this.gridView.OptionsCustomization.AllowSort           = true;

 

            // 셀

            this.gridView.OptionsView.ShowButtonMode                   = ShowButtonModeEnum.ShowForFocusedCell;

            this.gridView.OptionsView.AllowCellMerge                   = false;

            this.gridView.OptionsBehavior.EditorShowMode               = EditorShowMode.MouseDown;

            this.gridView.OptionsBehavior.Editable                     = true;

            this.gridView.OptionsSelection.EnableAppearanceFocusedCell = true;

            this.gridView.OptionsSelection.InvertSelection             = false;

            this.gridView.OptionsHint.ShowCellHints                    = false;

            this.gridView.OptionsClipboard.CopyColumnHeaders           = DefaultBoolean.False;

 

            // 모양

            this.gridView.Appearance.Row.Font         = Font;

            this.gridView.Appearance.HeaderPanel.Font = Font;

 

            GridColumn typeColumn = this.gridView.Columns.AddVisible("Type", "타입");

 

            typeColumn.Name                                    = "Type";

            typeColumn.Width                                   = 100;

            typeColumn.AppearanceHeader.Options.UseTextOptions = true;

            typeColumn.AppearanceHeader.TextOptions.HAlignment = HorzAlignment.Center;

            typeColumn.AppearanceHeader.TextOptions.VAlignment = VertAlignment.Center;

            typeColumn.AppearanceCell.Options.UseTextOptions   = true;

            typeColumn.AppearanceCell.TextOptions.HAlignment   = HorzAlignment.Near;

            typeColumn.AppearanceCell.TextOptions.VAlignment   = VertAlignment.Center;

            typeColumn.OptionsColumn.AllowEdit                 = true;

            typeColumn.ColumnEdit                              = typeRepositoryItemComboBox;

 

            GridColumn valueColumn = this.gridView.Columns.AddVisible("Value", "값");

 

            valueColumn.Name                                    = "Value";

            valueColumn.Width                                   = 200;

            valueColumn.AppearanceHeader.Options.UseTextOptions = true;

            valueColumn.AppearanceHeader.TextOptions.HAlignment = HorzAlignment.Center;

            valueColumn.AppearanceHeader.TextOptions.VAlignment = VertAlignment.Center;

            valueColumn.AppearanceCell.Options.UseTextOptions   = true;

            valueColumn.AppearanceCell.TextOptions.HAlignment   = HorzAlignment.Near;

            valueColumn.AppearanceCell.TextOptions.VAlignment   = VertAlignment.Center;

            valueColumn.OptionsColumn.AllowEdit                 = true;

            valueColumn.ColumnEdit                              = valueRepositoryItemComboBox;

 

            #endregion

            #region 이벤트를 설정한다.

 

            this.gridView.ShownEditor += gridView_ShownEditor;

 

            #endregion

 

            this.gridControl.DataSource = Item.GetList();

        }

 

        #endregion

 

        //////////////////////////////////////////////////////////////////////////////////////////////////// Method

        ////////////////////////////////////////////////////////////////////////////////////////// Private

        //////////////////////////////////////////////////////////////////////////////// Event

 

        #region 그리드 뷰 에디터 표시 후 처리하기 - gridView_ShownEditor(sender, e)

 

        /// <summary>

        /// 그리드 뷰 에디터 표시 후 처리하기

        /// </summary>

        /// <param name="sender">이벤트 발생자</param>

        /// <param name="e">이벤트 인자</param>

        private void gridView_ShownEditor(object sender, EventArgs e)

        {

            GridView gridView = sender as GridView;

 

            if(gridView.FocusedColumn.FieldName == nameof(Item.Value))

            {

                ComboBoxEdit comboBoxEdit = gridView.ActiveEditor as ComboBoxEdit;

 

                if(comboBoxEdit != null)

                {

                    Item item = gridView.GetFocusedRow() as Item;

 

                    if(item.Type == "타입 1")

                    {

                        comboBoxEdit.Properties.Items.Clear();

 

                        comboBoxEdit.Properties.Items.Add("타입 1 값 1");

                        comboBoxEdit.Properties.Items.Add("타입 1 값 2");

                        comboBoxEdit.Properties.Items.Add("타입 1 값 3");

                    }

                    else if(item.Type == "타입 2")

                    {

                        comboBoxEdit.Properties.Items.Clear();

 

                        comboBoxEdit.Properties.Items.Add("타입 2 값 1");

                        comboBoxEdit.Properties.Items.Add("타입 2 값 2");

                        comboBoxEdit.Properties.Items.Add("타입 2 값 3");

                    }

                }

            }

        }

 

        #endregion

    }

}

 

------------------------------------------------------------------------------------------------------------------------

Posted by 사용자 icodebroker

댓글을 달아 주세요