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

728x90
반응형
728x170

TestProject.zip
다운로드

▶ MainForm.cs

using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;

using DevExpress.Utils;
using DevExpress.XtraEditors;
using DevExpress.XtraGrid;
using DevExpress.XtraGrid.Views.Grid.ViewInfo;
using DevExpress.XtraTreeList;
using DevExpress.XtraTreeList.Nodes;

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

        #region Field

        /// <summary>
        /// 그리드 히트 정보
        /// </summary>
        private GridHitInfo gridHitInfo = null;

        /// <summary>
        /// 그리드 데이터 소스
        /// </summary>
        private BindingList<Person> gridDataSource = new BindingList<Person>();

        #endregion

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

        #region 생성자 - MainForm()

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

            this.gridDataSource.Add(new Person("John"   , "Smith"   , "USA"   ));
            this.gridDataSource.Add(new Person("Michael", "Suyama"  , "UK"    ));
            this.gridDataSource.Add(new Person("Laura"  , "Callahan", "UK"    ));
            this.gridDataSource.Add(new Person("Gerard" , "Blain"   , "France"));
            this.gridDataSource.Add(new Person("Sergio" , "Rubini"  , "Italy" ));
            this.gridDataSource.Add(new Person("Andrew" , "Fuller"  , "USA"   ));

            this.gridControl.DataSource = this.gridDataSource;

            this.gridView.OptionsBehavior.EditorShowMode = EditorShowMode.Click;

            this.treeList.OptionsView.ShowIndentAsRowStyle = true;
            this.treeList.AllowDrop                        = true;
            this.treeList.KeyFieldName                     = "ID";
            this.treeList.ParentFieldName                  = "ParentID";

            this.treeList.DataSource = new BindingList<PersonExtended>();

            this.gridControl.MouseDown += gridControl_MouseDown;
            this.gridControl.MouseMove += gridControl_MouseMove;
            this.treeList.DragEnter    += treeList_DragEnter;
            this.treeList.DragDrop     += treeList_DragDrop;
            this.treeList.DragOver     += treeList_DragOver;
        }

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Method
        ////////////////////////////////////////////////////////////////////////////////////////// Private

        #region 그리드 컨트롤 마우스 DOWN 처리하기 - gridControl_MouseDown(sender, e)

        /// <summary>
        /// 그리드 컨트롤 마우스 DOWN 처리하기
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private void gridControl_MouseDown(object sender, MouseEventArgs e)
        {
            this.gridHitInfo = this.gridView.CalcHitInfo(new Point(e.X, e.Y));
        }

        #endregion
        #region 그리드 컨트롤 마우스 이동시 처리하기 - gridControl_MouseMove(sender, e)

        /// <summary>
        /// 그리드 컨트롤 마우스 이동시 처리하기
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private void gridControl_MouseMove(object sender, MouseEventArgs e)
        {
            if(this.gridHitInfo == null)
            {
                return;
            }

            if(e.Button != MouseButtons.Left)
            {
                return;
            }

            Rectangle dragRectangle = new Rectangle
            (
                new Point
                (
                    this.gridHitInfo.HitPoint.X - SystemInformation.DragSize.Width  / 2,
                    this.gridHitInfo.HitPoint.Y - SystemInformation.DragSize.Height / 2
                ),
                SystemInformation.DragSize
            );

            if(!(this.gridHitInfo.RowHandle == GridControl.InvalidRowHandle) && !dragRectangle.Contains(new Point(e.X, e.Y)))
            {
                object data = this.gridView.GetRow(gridHitInfo.RowHandle);

                this.gridControl.DoDragDrop(data, DragDropEffects.Copy);
            }
        }

        #endregion
        #region 트리 리스트 드래그 ENTER 처리하기 - treeList_DragEnter(sender, e)

        /// <summary>
        /// 트리 리스트 드래그 ENTER 처리하기
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private void treeList_DragEnter(object sender, DragEventArgs e)
        {
            e.Effect = DragDropEffects.Copy;
        }

        #endregion
        #region 트리 리스트 드래그 OVER 처리하기 - treeList_DragOver(sender, e)

        /// <summary>
        /// 트리 리스트 드래그 OVER 처리하기
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private void treeList_DragOver(object sender, DragEventArgs e)
        {
            e.Effect = DragDropEffects.Copy;
        }

        #endregion
        #region 트리 리스트 드래그 DROP 처리하기 - treeList_DragDrop(sender, e)

        /// <summary>
        /// 트리 리스트 드래그 DROP 처리하기
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private void treeList_DragDrop(object sender, DragEventArgs e)
        {
            TreeList treeList = sender as TreeList;

            DXDragEventArgs args = treeList.GetDXDragEventArgs(e);

            DragInsertPosition position = args.DragInsertPosition;

            Person persion = e.Data.GetData(typeof(TestProject.Person)) as Person;

            if(persion == null)
            {
                return;
            }

            int parentID = (int)treeList.RootValue;

            TreeListNode node = args.TargetNode;

            if(node == null)
            {
                treeList.AppendNode((new PersonExtended(persion, parentID)).ToArray(), null);
            }
            else
            {
                if(position == DragInsertPosition.AsChild)
                {
                    parentID = Convert.ToInt32(node.GetValue("ID"));

                    object[] persionValueArray = (new PersonExtended(persion, parentID)).ToArray();

                    treeList.AppendNode(persionValueArray, node);
                }

                if(position == DragInsertPosition.Before)
                {
                    parentID = Convert.ToInt32(node.GetValue("ParentID"));

                    object[] persionValueArray = (new PersonExtended(persion, parentID)).ToArray();

                    TreeListNode newNode = treeList.AppendNode(persionValueArray, node.ParentNode);

                    int targetPosition;

                    if(node.ParentNode == null)
                    {
                        targetPosition = treeList.Nodes.IndexOf(node);
                    }
                    else
                    {
                        targetPosition = node.ParentNode.Nodes.IndexOf(node);
                    }

                    treeList.SetNodeIndex(newNode, targetPosition);
                }

                node.Expanded = true;
            }
        }

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

댓글을 달아 주세요