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

TestProject.zip
다운로드

▶ MainForm.cs

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

using DevExpress.Utils;
using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Controls;
using DevExpress.XtraEditors.Repository;
using DevExpress.XtraGrid;
using DevExpress.XtraGrid.Views.Base;
using DevExpress.XtraGrid.Views.Card;
using DevExpress.XtraGrid.Views.Card.ViewInfo;
using DevExpress.XtraGrid.Views.Grid;

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

        #region Field

        /// <summary>
        /// 데이터 셋
        /// </summary>
        private DataSet dataSet = null;

        #endregion

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

        #region 생성자 - MainForm()

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

            #region 데이터를 로드한다.

            OleDbConnection connection = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=..\\..\\nwind.mdb");

            OleDbDataAdapter categoryAdapter = new OleDbDataAdapter
            (
                "SELECT CategoryID, CategoryName, Picture FROM Categories",
                connection
            );

            OleDbDataAdapter productAdapter  = new OleDbDataAdapter
            (
                "SELECT CategoryID, ProductID, ProductName, UnitPrice FROM Products",
                connection
            );

            this.dataSet = new DataSet();

            categoryAdapter.Fill(this.dataSet, "Categories");
            productAdapter.Fill (this.dataSet, "Products"  );

            DataTable categoryTable = this.dataSet.Tables["Categories"];
            DataTable productTable  = this.dataSet.Tables["Products"  ];

            productTable.PrimaryKey = new DataColumn[] { productTable.Columns["ProductID"] };

            DataColumn keyColumn        = categoryTable.Columns["CategoryID"];
            DataColumn foreignKeyColumn = productTable.Columns ["CategoryID"];

            this.dataSet.Relations.Add("CategoriesProducts", keyColumn, foreignKeyColumn);

            #endregion
            #region 저장소 항목 픽처 에디터를 설정한다.

            RepositoryItemPictureEdit repositoryItemPictureEdit = new RepositoryItemPictureEdit();

            repositoryItemPictureEdit.SizeMode = PictureSizeMode.Stretch;

            #endregion

            this.gridControl.RepositoryItems.Add(repositoryItemPictureEdit);

            this.gridControl.AllowDrop = true;

            CardView cardView = new CardView(this.gridControl);

            cardView.ViewCaption = "Category Products";

            this.gridControl.LevelTree.Nodes.Add("CategoriesProducts", cardView);

            this.gridControl.DataSource = categoryTable;

            this.gridView.RowHeight = 50;

            this.gridView.Columns["CategoryID"].VisibleIndex = -1;
            
            this.gridView.Columns["Picture"].ColumnEdit               = repositoryItemPictureEdit;
            this.gridView.Columns["Picture"].OptionsColumn.FixedWidth = true;
            this.gridView.Columns["Picture"].Width                    = 180;

            cardView.PopulateColumns(productTable);

            cardView.Columns["CategoryID"].VisibleIndex = -1;

            cardView.Columns["UnitPrice"].DisplayFormat.FormatType   = FormatType.Numeric;
            cardView.Columns["UnitPrice"].DisplayFormat.FormatString = "c2";

            this.gridControl.DragOver += gridControl_DragOver;
            this.gridControl.DragDrop += gridControl_DragDrop;
            cardView.MouseDown        += cardView_MouseDown;
        }

        #endregion

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

        #region 그리드 컨트롤 DRAG OVER 처리하기 - gridControl_DragOver(sender, e)

        /// <summary>
        /// 그리드 컨트롤 DRAG OVER 처리하기
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private void gridControl_DragOver(object sender, DragEventArgs e)
        {
            GridControl gridControl = sender as GridControl;

            Point point = gridControl.PointToClient(new Point(e.X, e.Y));

            BaseView baseView = gridControl.GetViewAt(point);

            if(baseView is CardView)
            {
                e.Effect = DragDropEffects.Move;
            }
            else
            {
                e.Effect = DragDropEffects.None;
            }
        }

        #endregion
        #region 그리드 컨트롤 DRAG DROP 처리하기 - gridControl_DragDrop(sender, e)

        /// <summary>
        /// 그리드 컨트롤 DRAG DROP 처리하기
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private void gridControl_DragDrop(object sender, DragEventArgs e)
        {
            GridControl gridControl = sender as GridControl;

            Point point = gridControl.PointToClient(new Point(e.X, e.Y));

            CardView cardView = gridControl.GetViewAt(point) as CardView;

            string dragData = e.Data.GetData(DataFormats.Text).ToString();

            int productID = Convert.ToInt32(dragData);

            GridView gridView = cardView.ParentView as GridView;

            object categoryIDObject = gridView.GetRowCellValue
            (
                cardView.SourceRowHandle, 
                gridView.Columns["CategoryID"]
            );

            int categoryID = Convert.ToInt32(categoryIDObject);

            DataRow dataRow = this.dataSet.Tables["Products"].Rows.Find(productID);
            
            if(dataRow != null)
            {
                dataRow.SetField("CategoryID", categoryID);
            }
        }

        #endregion
        #region 카드 뷰 마우스 DOWN 처리하기 - cardView_MouseDown(sender, e)

        /// <summary>
        /// 카드 뷰 마우스 DOWN 처리하기
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private void cardView_MouseDown(object sender, MouseEventArgs e)
        {
            CardView cardView = sender as CardView;
            GridView gridView = cardView.ParentView as GridView;

            CardHitInfo cardHitInfo = cardView.CalcHitInfo(e.Location);

            if(cardHitInfo.HitTest == CardHitTest.CardCaption)
            {
                string productID = cardView.GetRowCellValue
                (
                    cardHitInfo.RowHandle,
                    cardView.Columns["ProductID"]
                ).ToString();

                this.gridControl.DoDragDrop(productID, DragDropEffects.Move);
            }
        }

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

댓글을 달아 주세요