자료실

부자는 돈을 써서 시간을 아끼지만 가난한 사람은 시간을 써서 돈을 아낀다

vb.net

IT HUB를 찾아주셔서 감사합니다.

Controls [VB.NET] DataGridView 컨트롤에서 행 끌어서 이동

페이지 정보

profile_image
작성자 하나를하더라도최선을
댓글 0건 조회 7,926회 작성일 20-02-23 16:54

본문

    Private fromIndex As Integer
    Private dragIndex As Integer
    Private dragRect As Rectangle
    Private Sub DataGridView1_DragDrop(ByVal sender As Object,
                                   ByVal e As DragEventArgs) _
                                   Handles DataGridView1.DragDrop
        Dim p As Point = DataGridView1.PointToClient(New Point(e.X, e.Y))
        dragIndex = DataGridView1.HitTest(p.X, p.Y).RowIndex
        If (e.Effect = DragDropEffects.Move) Then
            Dim dragRow As DataGridViewRow = e.Data.GetData(GetType(DataGridViewRow))
            DataGridView1.Rows.RemoveAt(fromIndex)
            DataGridView1.Rows.Insert(dragIndex, dragRow)
        End If
    End Sub
 
    Private Sub DataGridView1_DragOver(ByVal sender As Object,
                                   ByVal e As DragEventArgs) _
                                   Handles DataGridView1.DragOver
        e.Effect = DragDropEffects.Move
    End Sub
    Private Sub DataGridView1_MouseDown(ByVal sender As Object,
                                    ByVal e As MouseEventArgs) _
                                    Handles DataGridView1.MouseDown
        fromIndex = DataGridView1.HitTest(e.X, e.Y).RowIndex
        If fromIndex > -1 Then
            Dim dragSize As Size = SystemInformation.DragSize
            dragRect = New Rectangle(New Point(e.X - (dragSize.Width / 2),
                                               e.Y - (dragSize.Height / 2)),
                                     dragSize)
        Else
            dragRect = Rectangle.Empty
        End If
    End Sub
 
    Private Sub DataGridView1_MouseMove(ByVal sender As Object,
                                        ByVal e As MouseEventArgs) _
                                        Handles DataGridView1.MouseMove
        If (e.Button And MouseButtons.Left) = MouseButtons.Left Then
            If (dragRect <> Rectangle.Empty _
            AndAlso Not dragRect.Contains(e.X, e.Y)) Then
                DataGridView1.DoDragDrop(DataGridView1.Rows(fromIndex),
                                         DragDropEffects.Move)
 
            End If
        End If
    End Sub

댓글목록

등록된 댓글이 없습니다.