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

728x90
반응형
728x170

TestProject.zip
다운로드

▶ MainForm.cs

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

using DevExpress.Data;
using DevExpress.Utils;
using DevExpress.XtraGrid.Columns;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraGrid.Views.Grid.ViewInfo;

namespace TestProject
{
    /// <summary>
    /// 테스트 폼
    /// </summary>
    public partial class MainForm : Form
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor
        ////////////////////////////////////////////////////////////////////////////////////////// Public

        #region 생성자 - TestForm()

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

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

            Load                            += Form_Load;
            this.sourceGridView.MouseDown   += sourceGridView_MouseDown;
            this.targetGridControl.DragDrop += targetGridControl_DragDrop;
            this.targetGridControl.DragOver += targetGridControl_DragOver;
        }

        #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)
        {
            SetGridView(this.sourceGridView, "sourceGridView", false, false, false, GridMultiSelectMode.CellSelect, false, false);

            this.sourceGridView.Columns.Add
            (
                GetGridColumn
                (
                    "ID",
                    "ID",
                    UnboundColumnType.Integer,
                    100,
                    HorzAlignment.Far,
                    false,
                    FormatType.None,
                    null
                )
            );
            this.sourceGridView.Columns.Add
            (
                GetGridColumn
                (
                    "명칭",
                    "Name",
                    UnboundColumnType.Integer,
                    100,
                    HorzAlignment.Near,
                    false,
                    FormatType.None,
                    null
                )
            );

            SetGridView(this.targetGridView, "targetGridView", false, false, false, GridMultiSelectMode.CellSelect, false, false);

            this.targetGridView.Columns.Add
            (
                GetGridColumn
                (
                    "ID",
                    "ID",
                    UnboundColumnType.Integer,
                    100,
                    HorzAlignment.Far,
                    false,
                    FormatType.None,
                    null
                )
            );
            this.targetGridView.Columns.Add
            (
                GetGridColumn
                (
                    "명칭",
                    "Name",
                    UnboundColumnType.Integer,
                    100,
                    HorzAlignment.Near,
                    false,
                    FormatType.None,
                    null
                )
            );

            DataTable sourceTable = GetEmptyList();

            SetList(sourceTable);

            this.sourceGridControl.DataSource = sourceTable;

            DataTable targetTable = GetEmptyList();

            this.targetGridControl.DataSource = targetTable;
        }

        #endregion
        #region 소스 마우스 버튼 DOWN 처리하기 - sourceGridView_MouseDown(sender, e)

        /// <summary>
        /// 소스 마우스 버튼 DOWN 처리하기
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private void sourceGridView_MouseDown(object sender, MouseEventArgs e)
        {
            GridHitInfo gridHitInfo = this.sourceGridView.CalcHitInfo(e.Location);

            if(gridHitInfo.InRowCell)
            {
                this.sourceGridView.FocusedRowHandle = gridHitInfo.RowHandle;

                DataRow row = this.sourceGridView.GetDataRow(gridHitInfo.RowHandle);

                this.sourceGridControl.DoDragDrop(row, DragDropEffects.Move);
            }
        }

        #endregion
        #region 타겟 목록 드래그 오버시 처리하기 - targetGridControl_DragOver(sender, e)

        /// <summary>
        /// 타겟 목록 드래그 오버시 처리하기
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private void targetGridControl_DragOver(object sender, DragEventArgs e)
        {
            if(e.Data.GetDataPresent(typeof(DataRow)))
            {
                if((e.AllowedEffect & DragDropEffects.Move) != 0)
                {
                    e.Effect = DragDropEffects.Move;
                }
            }
            else
            {
                e.Effect = DragDropEffects.None;
            }
        }

        #endregion
        #region 타겟 목록 드래그 드롭시 처리하기 - targetGridControl_DragDrop(sender, e)

        /// <summary>
        /// 타겟 목록 드래그 드롭시 처리하기
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private void targetGridControl_DragDrop(object sender, DragEventArgs e)
        {
            if(e.Data.GetDataPresent(typeof(DataRow)))
            {
                DataRow sourceRow = e.Data.GetData(typeof(DataRow)) as DataRow;

                DataTable targetTable = this.targetGridControl.DataSource as DataTable;

                DataRow targetRow = targetTable.NewRow();
    
                for(int i = 0; i < targetTable.Columns.Count; i++)
                {
                    targetRow[i] = sourceRow[i];
                }

                targetTable.Rows.Add(targetRow);

                DataTable sourceTable = this.sourceGridControl.DataSource as DataTable;

                sourceTable.Rows.Remove(sourceRow);
            }
        }

        #endregion

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

        #region 그리드 뷰 설정하기 - SetGridView(gridView, name, sortable, editable, multiSelect, gridMultiSelectMode, showGroupPanel, showIndicator)

        /// <summary>
        /// 그리드 뷰 설정하기
        /// </summary>
        /// <param name="gridView">GridView</param>
        /// <param name="name">명칭</param>
        /// <param name="sortable">정렬 가능 여부</param>
        /// <param name="editable">편집 가능 여부</param>
        /// <param name="multiSelect">복수 선택 여부</param>
        /// <param name="gridMultiSelectMode">그리드 복수 선택 모드</param>
        /// <param name="showGroupPanel">그룹 패널 표시 여부</param>
        /// <param name="showIndicator">지시자 표시 여부</param>
        /// <returns>그리드 뷰</returns>
        private void SetGridView
        (
            GridView            gridView,
            string              name,
            bool                sortable,
            bool                editable,
            bool                multiSelect,
            GridMultiSelectMode gridMultiSelectMode,
            bool                showGroupPanel,
            bool                showIndicator
        )
        {
            gridView.Name                                     = name;
            gridView.OptionsBehavior.AutoPopulateColumns      = false;
            gridView.OptionsBehavior.Editable                 = editable;
            gridView.OptionsBehavior.EditorShowMode           = EditorShowMode.MouseDown;
            gridView.OptionsCustomization.AllowColumnMoving   = false;
            gridView.OptionsCustomization.AllowColumnResizing = true;
            gridView.OptionsCustomization.AllowFilter         = false;
            gridView.OptionsCustomization.AllowSort           = sortable;
            gridView.OptionsMenu.EnableColumnMenu             = false;
            gridView.OptionsSelection.MultiSelect             = multiSelect;
            gridView.OptionsSelection.MultiSelectMode         = gridMultiSelectMode;
            gridView.OptionsView.ColumnAutoWidth              = false;
            gridView.OptionsView.EnableAppearanceEvenRow      = true;
            gridView.OptionsView.ShowGroupPanel               = showGroupPanel;
            gridView.OptionsView.ShowIndicator                = showIndicator;
        }

        #endregion
        #region 그리드 컬럼 구하기 - GetGridColumn(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 GridColumn GetGridColumn
        (
            string            caption,
            string            fieldName,
            UnboundColumnType unboundColumnType,
            int               width,
            HorzAlignment     horzAlignment,
            bool              allowEdit,
            FormatType        formatType,
            string            formatString
        )
        {
            GridColumn gridColumn = new GridColumn();

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

            return gridColumn;
        }

        #endregion

        #region 비어있는 목록 구하기 - GetEmptyList()

        /// <summary>
        /// 비어있는 목록 구하기
        /// </summary>
        /// <returns>비어있는 목록</returns>
        private DataTable GetEmptyList()
        {
            DataTable table = new DataTable();

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

            return table;
        }

        #endregion
        #region 목록 설정하기 - SetList(sourceTable)

        /// <summary>
        /// 목록 설정하기
        /// </summary>
        /// <param name="sourceTable">소스 테이블</param>
        private void SetList(DataTable sourceTable)
        {
            DataRow row = null;

            for(int i = 1; i < 101; i++)
            {
                row = sourceTable.NewRow();

                row["ID"  ] = i;
                row["Name"] = "목록 " + i.ToString();

                sourceTable.Rows.Add(row);
            }
        }

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

댓글을 달아 주세요