아래 내용은 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);
            }
        }

1.       

        private Bitmap ConvertDataGridViewToBmp1(DataGridView dg)
        {
            int height = dg.Height;
            dg.Height = dg.RowCount * dg.RowTemplate.Height;

            Bitmap bitmap = new Bitmap(dg.Width, dg.Height);
            dg.DrawToBitmap(bitmap, new Rectangle(0, 0, dg.Width, dg.Height));

            dg.Height = height;

            return bitmap;
        }

 

2.

        [DllImport("gdi32.dll", ExactSpelling = true, CharSet = CharSet.Auto, SetLastError = true)]
        private static extern bool BitBlt(IntPtr pHdc, int iX, int iY, int iWidth, int iHeight, IntPtr pHdcSource, int iXSource, int iYSource, System.Int32 dw);
        private const int src = 0xCC0020;

        private Bitmap ConvertDataGridViewToBmp2(DataGridView dg)
        {
            this.dataGridView.ColumnHeadersVisible = false;
            this.dataGridView.RowHeadersVisible = false;

            dg.Refresh();
            dg.Select();

            Graphics g = dg.CreateGraphics();
            Bitmap bitmap = new Bitmap(dg.ClientSize.Width, dg.ClientSize.Height, g);
            Graphics g = Graphics.FromImage(bitmap);
            IntPtr bitmapHoc = g.GetHdc();
            IntPtr gHoc = g.GetHdc();

            BitBlt(bitmapHoc, 0, 0, dg.ClientSize.Width, dg.ClientSize.Height, gHoc, 0, 0, src);

            g.ReleaseHdc(gHoc);

            g.ReleaseHdc(bitmapHoc);

            return bitmap;
        }

+ Recent posts

티스토리 툴바