Language/C#

바인딩 된 datagridview에서 마우스 드래그로 컬럼 순서 변경

비비이잉 2022. 7. 4. 13:52
반응형

바인딩이 되어있는 DataGridView에서 마우스 드래그 이벤트를 통해서 컬럼 순서(위치)를 변경할 수 있는 방법 

Row에 해당하는 내용은 구글링을 해도 많이 나오지만, Column에 해당하는 내용은 거의 없었다 

응용해서 코드를 수정해본 내용이다 .

 

 

private Rectangle dragBoxFromMouseDown;
        private int columnIndexFromMouseDown;
        private int columnIndexOfItemUnderMouseToDrop;
private void metroGridWaferReview_MouseMove(object sender, MouseEventArgs e)
        {
            if ((e.Button & MouseButtons.Left) == MouseButtons.Left)
            {
                if (dragBoxFromMouseDown != Rectangle.Empty &&
                    !dragBoxFromMouseDown.Contains(e.X, e.Y))
                {
                    DragDropEffects dropEffect = metroGridWaferReview.DoDragDrop(metroGridWaferReview.Columns[columnIndexFromMouseDown], DragDropEffects.Move);
                }
            }
        }

        private void metroGridWaferReview_MouseDown(object sender, MouseEventArgs e)
        {
            columnIndexFromMouseDown = metroGridWaferReview.HitTest(e.X, e.Y).ColumnIndex;
            if (columnIndexFromMouseDown != -1)
            {
                Size dragSize = SystemInformation.DragSize;

                dragBoxFromMouseDown = new Rectangle(new Point(e.X - (dragSize.Width / 2), e.Y - (dragSize.Height / 2)), dragSize);
            }
            else
                dragBoxFromMouseDown = Rectangle.Empty;
        }

        

        private void metroGridWaferReview_DragDrop(object sender, DragEventArgs e)
        {
            Point clientPoint = metroGridWaferReview.PointToClient(new Point(e.X, e.Y));
            columnIndexOfItemUnderMouseToDrop = metroGridWaferReview.HitTest(clientPoint.X, clientPoint.Y).ColumnIndex; 
            string down = metroGridWaferReview.Columns[columnIndexFromMouseDown].Name.ToString();
            string drop = metroGridWaferReview.Columns[columnIndexOfItemUnderMouseToDrop].Name.ToString();
            int down_displayindex = metroGridWaferReview.Columns[down].DisplayIndex; 
            int drop_displayindex = metroGridWaferReview.Columns[drop].DisplayIndex; 
            List<string> list_columnname = new List<string>();
            if (e.Effect == DragDropEffects.Move)
            {
                for(int i=0; i < metroGridWaferReview.Columns.Count; i++)
                {
                    if (down_displayindex < metroGridWaferReview.Columns[i].DisplayIndex && metroGridWaferReview.Columns[i].DisplayIndex <= drop_displayindex)
                    {
                        list_columnname.Add(metroGridWaferReview.Columns[i].Name);
                    }
                }
                for (int i = 0; i < list_columnname.Count; i++)
                    metroGridWaferReview.Columns[list_columnname[i]].DisplayIndex -= 1;
                metroGridWaferReview.Columns[down].DisplayIndex = drop_displayindex;
            }
        }

        private void metroGridWaferReview_DragOver(object sender, DragEventArgs e)
        {
            e.Effect = DragDropEffects.Move;
        }

 

https://424485.tistory.com/75

 

C# 바인딩된 Datagridview 마우스 드래그로 순서 변경하기

private Rectangle dragBoxFromMouseDown; private int rowIndexFromMouseDown; private int rowIndexOfItemUnderMouseToDrop; private void ItemGrid_MouseMove(object sender, MouseEventArgs e) { if ((e.Butto..

424485.tistory.com

https://icodebroker.tistory.com/3516

 

[DEVEXPRESS/WINFORM] GridControl 클래스 : DoDragDrop 메소드를 사용해 내부에서 마우스 드래그 & 드롭 처리

using System.Data; using System.Drawing; using System.ComponentModel; using System.Windows.Forms; using DevExpress.XtraGrid; using DevExpress.XtraGrid.Views.Grid; using DevExpress.XtraGrid.Views.Gri..

icodebroker.tistory.com

 

반응형