none
DataGridView Column DragAndDrop and Resizing RRS feed

  • Question

  • Hi All

    In my WinForm I use DragDrop  to Reorder column DisplayIndexas follow

      Private Sub DGV_MouseDown(sender As Object, e As MouseEventArgs) Handles DGV.MouseDown
            Dim hitCell As DataGridView.HitTestInfo = DGV.HitTest(e.X, e.Y)
            If hitCell.RowIndex > -1 Then
                Return
            End If
            ColumnToMove = DGV.Columns(hitCell.ColumnIndex)
            ColumnToMoveIndex = hitCell.ColumnIndex
            ColumnToMoveDisplayIndex = DGV.Columns(hitCell.ColumnIndex).DisplayIndex
            DGV.DoDragDrop(ColumnToMove, DragDropEffects.Move)
        End Sub
        Private Sub DGV_DragEnter(ByVal sender As Object, ByVal e As DragEventArgs) Handles DGV.DragEnter
            e.Effect = DragDropEffects.Move
        End Sub
    
        Private Sub DGV_DragDrop(ByVal sender As Object, ByVal e As DragEventArgs) Handles DGV.DragDrop
            Dim clientPoint As Point = DGV.PointToClient(New Point(e.X, e.Y))
            Dim hitCell As DataGridView.HitTestInfo = DGV.HitTest(clientPoint.X, clientPoint.Y)
            If hitCell.RowIndex > -1 Then
                Return
            End If
            ColumnToReplaceIndex = hitCell.ColumnIndex
            ColumnToReplaceDisplayIndex = DGV.Columns(hitCell.ColumnIndex).DisplayIndex
            ' se colonna iniziale e finale sono la stessa allora fai sort sulla colonna
            If ColumnToReplaceIndex = ColumnToMoveIndex Then
                DGV.Sort(ColumnToMove, DGVSortDirection)
            End If
            ' scambia i DisplayIndex delle due colonne
            DGV.Columns(ColumnToMoveIndex).DisplayIndex = ColumnToReplaceDisplayIndex
            DGV.Columns(ColumnToReplaceIndex).DisplayIndex = ColumnToMoveDisplayIndex
    
        End Sub
    

    Every Thing is OK. It works.

    The problem is that I cannot resizing a Column with Mouse since when the mouse is down a Column

    a DragDrop start.

    How to solve the Problem ?

    Thank for Help

    Tuesday, November 6, 2018 11:16 PM

Answers

  • Hi Alex

    Prolem solved checking X coordinate of mouse 

    If X si too much near the column divider(6 pixels) I don't use DragDrop so the user can resize the column

    Best Regards

    Claudio

    • Marked as answer by Claudio111 Wednesday, November 7, 2018 10:32 AM
    Wednesday, November 7, 2018 9:52 AM

All replies

  • Hi,

    Do you want to DragDrop  to Reorder column DisplayIndexas?I don't think you need to code below,just   set DGV.AllowUserToOrderColumns=true.

    Private Sub DGV_MouseDown(sender As Object, e As MouseEventArgs) Handles DGV.MouseDown
            Dim hitCell As DataGridView.HitTestInfo = DGV.HitTest(e.X, e.Y)
            If hitCell.RowIndex > -1 Then
                Return
            End If
            ColumnToMove = DGV.Columns(hitCell.ColumnIndex)
            ColumnToMoveIndex = hitCell.ColumnIndex
            ColumnToMoveDisplayIndex = DGV.Columns(hitCell.ColumnIndex).DisplayIndex
            DGV.DoDragDrop(ColumnToMove, DragDropEffects.Move)
        End Sub
        Private Sub DGV_DragEnter(ByVal sender As Object, ByVal e As DragEventArgs) Handles DGV.DragEnter
            e.Effect = DragDropEffects.Move
        End Sub
    
        Private Sub DGV_DragDrop(ByVal sender As Object, ByVal e As DragEventArgs) Handles DGV.DragDrop
            Dim clientPoint As Point = DGV.PointToClient(New Point(e.X, e.Y))
            Dim hitCell As DataGridView.HitTestInfo = DGV.HitTest(clientPoint.X, clientPoint.Y)
            If hitCell.RowIndex > -1 Then
                Return
            End If
            ColumnToReplaceIndex = hitCell.ColumnIndex
            ColumnToReplaceDisplayIndex = DGV.Columns(hitCell.ColumnIndex).DisplayIndex
            ' se colonna iniziale e finale sono la stessa allora fai sort sulla colonna
            If ColumnToReplaceIndex = ColumnToMoveIndex Then
                DGV.Sort(ColumnToMove, DGVSortDirection)
            End If
            ' scambia i DisplayIndex delle due colonne
            DGV.Columns(ColumnToMoveIndex).DisplayIndex = ColumnToReplaceDisplayIndex
            DGV.Columns(ColumnToReplaceIndex).DisplayIndex = ColumnToMoveDisplayIndex
    
        End Sub

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Claudio111 Wednesday, November 7, 2018 7:53 AM
    • Unmarked as answer by Claudio111 Wednesday, November 7, 2018 7:53 AM
    Wednesday, November 7, 2018 2:15 AM
  • Hi,

    I know it but I use Double Buffer for DGV and the standard AllowUserToOrderColumn doesn't give good graphics effect.

    So I decide to use DragDrop in the code.

    Now I have to solve Resizing problem in my question

    Bye

    Claudio

     

    Wednesday, November 7, 2018 8:01 AM
  • Hi Alex

    Prolem solved checking X coordinate of mouse 

    If X si too much near the column divider(6 pixels) I don't use DragDrop so the user can resize the column

    Best Regards

    Claudio

    • Marked as answer by Claudio111 Wednesday, November 7, 2018 10:32 AM
    Wednesday, November 7, 2018 9:52 AM