728x90
728x170

아래 내용은 DataGridView 의 row 를 마우스 드래그로 이동 시킬수 있는 코드입니다.

단, 직접 DataGridView 에 데이터를 작성해야 합니다. (아래 코드참고)

DataSource 에 데이터를 넣으면 에러가 발생됩니다.

            dataGridView.ColumnCount = 2;

            dataGridView.Columns[0].Name = "First";
            dataGridView.Columns[1].Name = "Last";
            dataGridView.Columns["First"].HeaderText = "First Name";
            dataGridView.Columns["Last"].HeaderText = "Last Name";
            dataGridView.Columns["First"].SortMode = DataGridViewColumnSortMode.Programmatic;
            dataGridView.Columns["Last"].SortMode = DataGridViewColumnSortMode.Programmatic;

            dataGridView.Rows.Add(new string[] { "공사명", "땡땡대학교 복도" });
            dataGridView.Rows.Add(new string[] { "공종", "토공" });
            dataGridView.Rows.Add(new string[] { "위치", "자재배송" });
            dataGridView.Rows.Add(new string[] { "내용", "노상다짐. 검측" });
            dataGridView.Rows.Add(new string[] { "일자", "2013.07" });

먼저 DataGridView 에 아래 처럼 이벤트를 먹이고 AllowDrop 을 True 합니다.

            this.dataGridView.MouseMove += dataGridView_MouseMove;
            this.dataGridView.MouseDown += dataGridView_MouseDown;
            this.dataGridView.DragOver += dataGridView_DragOver;
            this.dataGridView.DragDrop += dataGridView_DragDrop;
            this.dataGridView.AllowDrop = true;

그리고 아래 처럼 각 이벤트에 아래 처럼 코딩하면 row 를 이동시킬 수 있습니다.

        private Rectangle dragBoxFromMouseDown;
        private int rowIndexFromMouseDown;
        private int rowIndexOfItemUnderMouseToDrop;
        private void dataGridView_MouseMove(object sender, MouseEventArgs e)
        {
            if ((e.Button & MouseButtons.Left) == MouseButtons.Left)
            {
                if (dragBoxFromMouseDown != Rectangle.Empty &&
                    !dragBoxFromMouseDown.Contains(e.X, e.Y))
                {

                    DragDropEffects dropEffect = dataGridView.DoDragDrop(dataGridView.Rows[rowIndexFromMouseDown], DragDropEffects.Move);
                }
            }
        }
        private void dataGridView_MouseDown(object sender, MouseEventArgs e)
        {
            rowIndexFromMouseDown = dataGridView.HitTest(e.X, e.Y).RowIndex;
            if (rowIndexFromMouseDown != -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 dataGridView_DragOver(object sender, DragEventArgs e)
        {
            e.Effect = DragDropEffects.Move;
        }
        private void dataGridView_DragDrop(object sender, DragEventArgs e)
        {
 
            Point clientPoint = dataGridView.PointToClient(new Point(e.X, e.Y));

            rowIndexOfItemUnderMouseToDrop = dataGridView.HitTest(clientPoint.X, clientPoint.Y).RowIndex;

            if (e.Effect == DragDropEffects.Move)
            {
                DataGridViewRow rowToMove = e.Data.GetData(typeof(DataGridViewRow)) as DataGridViewRow;
                dataGridView.Rows.RemoveAt(rowIndexFromMouseDown);
                dataGridView.Rows.Insert(rowIndexOfItemUnderMouseToDrop, rowToMove);
            }
        }

728x90
그리드형
Posted by kjun.kr
,