Question How to implement Drag & Drop between 2 datagrids?

VBStarterKid

Member
Joined
Aug 1, 2007
Messages
13
Location
Malaysia
Programming Experience
Beginner
Hi there, I would appreciate if someone could guide me on how to implement the drag & drop function that we always see in Windows apps.

I have a Windows Form with 2 grids. Besides using the double-clicking a record method to transfer the particular record to the other grid, I would like to also implement the drag and drop record from a grid to another. But I don't know how to start, though I have a faint idea it all has something to to with the OnDragDrop event or something similar..

Thank you for the help.
 
Quick search found this. Seemed to work fine when I tried it out.

VB.NET:
Public Class FrmDragGrid
    ' Some Properties that I have set for the Grid
    ' SelectionMode = FullRowSelect [For both the Grids]
    ' AllowDrop = True [For Grid DGV2]
    '
    '
    Dim DT1 As DataTable
    Dim DT2 As DataTable

    Private Sub FrmDragGrid_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        FillDataInGrids()
    End Sub

    Private Sub FillDataInGrids()
        DT1 = New DataTable
        DT2 = New DataTable

        'The First Table has four columns
        DT1.Columns.Add("Name", Type.GetType("System.String"))
        DT1.Columns.Add("Designation", Type.GetType("System.String"))
        DT1.Columns.Add("Department", Type.GetType("System.String"))
        DT1.Columns.Add("Salary", Type.GetType("System.String"))

        'Second has only two
        DT2.Columns.Add("Name", Type.GetType("System.String"))
        DT2.Columns.Add("Designation", Type.GetType("System.String"))

        'Now Add some Rows in the first DataTable
        Dim Dr As DataRow

        Dr = DT1.NewRow
        Dr("Name") = "Tom"
        Dr("Designation") = "Developer"
        Dr("Department") = "Engg"
        Dr("Salary") = "1000"
        DT1.Rows.Add(Dr)

        Dr = DT1.NewRow
        Dr("Name") = "Jerry"
        Dr("Designation") = "Developer"
        Dr("Department") = "Engg"
        Dr("Salary") = "1000"
        DT1.Rows.Add(Dr)

        Dr = DT1.NewRow
        Dr("Name") = "Micky"
        Dr("Designation") = "Analyst"
        Dr("Department") = "Engg"
        Dr("Salary") = "2000"
        DT1.Rows.Add(Dr)

        Dr = DT1.NewRow
        Dr("Name") = "Mini"
        Dr("Designation") = "Analyst"
        Dr("Department") = "Engg"
        Dr("Salary") = "2000"
        DT1.Rows.Add(Dr)

        Dr = DT1.NewRow
        Dr("Name") = "Donald"
        Dr("Designation") = "Manager"
        Dr("Department") = "Engg"
        Dr("Salary") = "3000"
        DT1.Rows.Add(Dr)

        'Now Bind the DataGrids to these table
        DGV1.DataSource = DT1
        DGV2.DataSource = DT2

    End Sub

    Private Sub DGV1_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles DGV1.MouseDown
        'Get the Index of Row which is being Dragged
        'We would use this Index on Drop to identify which Row was dragged and get the values from that row
        Dim Index As Integer
        Index = DGV1.HitTest(e.X, e.Y).RowIndex
        If Index > -1 Then
            'Pass the Index as "Data" argument of the DoDragDrop Function
            DGV1.DoDragDrop(Index, DragDropEffects.Move)
        End If
        'NOTE: A better approach would be to Get the Primary Key value of the Row and instead of Index
        'Set this Primary Key value in the Data argument of the DoDragDrop, and in DragDrop event get the Primary Key 
        'From the argument and based on the Primary Key get the values from the DataTable
    End Sub

    Private Sub DGV2_DragOver(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles DGV2.DragOver
        'Just to Show a mouse icon to denote drop is allowed here
        e.Effect = DragDropEffects.Move
    End Sub

    Private Sub DGV2_DragDrop(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles DGV2.DragDrop
        Try
            'Get the Index value that we stored in the Data in the MouseDown event
            'In case we stored a PrimaryKey value here in place of Index, we would get that by Type casting it to its type
            Dim index As Integer = Convert.ToInt32(e.Data.GetData(Type.GetType("System.Int32")))

            'Now based on the Index get the data in the Cells
            'Again if we had Primary Key value here we would have used the underlying DataTable DT1 to get the data for that key
            Dim Name As String
            Dim Desig As String
            Name = DGV1.Rows(index).Cells("Name").Value.ToString
            Desig = DGV1.Rows(index).Cells("Designation").Value.ToString
            Dim Dr As DataRow
            Dr = DT2.NewRow
            Dr("Name") = Name
            Dr("Designation") = Desig
            DT2.Rows.Add(Dr)

        Catch ex As Exception
            MsgBox(ex.Message)
        End Try

    End Sub

End Class
 
Back
Top