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


Reply With Quote
