Results 1 to 4 of 4

Thread: [2008] Datagridview drag drop : No duplicates

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Jun 2008
    Posts
    515

    [2008] Datagridview drag drop : No duplicates

    I am trying to drag & drop rows from datagridview1 to datagridview2 using the code below.

    My question is : how do I check for duplicates in the target datagridview before completing the drop.

    ie: If Jerry has already been drag dropped to datagridview2 then say, "Jerry is already in the list. Are you sure you want to add a duplicate ?"

    Thanks for any help.

    Code:
    Public Class frmDataGrid
        ' Some Properties that I have set for the Grid
        ' SelectionMode = FullRowSelect [For both the Grids]
        ' AllowDrop = True [For Grid DataGridView2]
        '
        '
        Dim DT1 As DataTable
        Dim DT2 As DataTable
    
        Private Sub frmDataGrid_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 DataGridView1_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles DataGridView1.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 = DataGridView1.HitTest(e.X, e.Y).RowIndex
            If Index > -1 Then
                'Pass the Index as "Data" argument of the DoDragDrop Function
                DataGridView1.DoDragDrop(Index, DragDropEffects.Move)
            End If
          End Sub
    
        Private Sub DataGridView2_DragOver(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles DataGridView2.DragOver
            'Just to Show a mouse icon to denote drop is allowed here
            e.Effect = DragDropEffects.Move
        End Sub
    
        Private Sub DataGridView2_DragDrop(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles DataGridView2.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 = DataGridView1.Rows(index).Cells("Name").Value.ToString
                Desig = DataGridView1.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

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: [2008] Datagridview drag drop : No duplicates

    The DataTable's Select method will return the rows that match a set of criteria. If Select returns an empty array then are no matching rows, otherwise there are.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Jun 2008
    Posts
    515

    Re: [2008] Datagridview drag drop : No duplicates

    Thanks for that pointer.

    How will this method differ if datagridview1 is databound and datagridview2 is unbound ?

    Can I still use the datatable method ?

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: [2008] Datagridview drag drop : No duplicates

    You want to know whether the destination grid already contains the data so the destination grid must be bound to a DataTable. The source is irrelevant. If not then you'll just have to check the destination in a loop.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width