mlsrinivas
New member
- Joined
- Aug 9, 2012
- Messages
- 2
- Programming Experience
- Beginner
Hello,
I am trying to design a form with two datagridviews on it and enable drag and drop between. The user should be able to drag and drop rows from grid1 to grid2 and vice versa. When a row is drag and dropped it should be removed from the source grid and added to the target grid.
One way dragdrop is happening but when trying toway method, mouse click on the grid is firing dragdrop event and creating problems. I am posting my code below please help me complete the task.
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
DataGridView1.Rows.Add(1, "Emp1", "Team Lead", "Y")
DataGridView1.Rows.Add(2, "Emp2", "Team Lead", "N")
DataGridView1.Rows.Add(3, "Emp3", "Team Lead", "Y")
DataGridView1.Rows.Add(4, "Emp4", "Team Lead", "Y")
DataGridView1.Rows.Add(5, "Emp5", "Team Lead", "N")
DataGridView1.Rows.Add(6, "Emp6", "Team Lead", "Y")
End Sub
Private Sub DataGridView1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles DataGridView1.MouseDown
If e.Button = Windows.Forms.MouseButtons.Left Then
If DataGridView1.SelectedRows.Count <> 0 Then
Dim i As Integer
For i = DataGridView1.SelectedRows.Count - 1 To 0 Step -1
If DataGridView1.SelectedRows(i).Cells("Flag").Value = "Y" Then
DataGridView1.DoDragDrop(DataGridView1.SelectedRows(i), DragDropEffects.All)
End If
Next
End If
End If
End Sub
Private Sub DataGridView2_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles DataGridView2.MouseDown
If e.Button = Windows.Forms.MouseButtons.Left Then
If DataGridView2.SelectedRows.Count <> 0 Then
Dim i As Integer
For i = DataGridView2.SelectedRows.Count - 1 To 0 Step -1
If DataGridView2.SelectedRows(i).Cells("DestFlag").Value = "Y" Then
DataGridView2.DoDragDrop(DataGridView2.SelectedRows(i), DragDropEffects.All)
End If
Next
End If
End If
End Sub
Private Sub DataGridView2_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles DataGridView2.DragDrop
Dim s As DataGridViewRow = e.Data.GetData(GetType(DataGridViewRow))
DataGridView2.Rows.Add(s.Cells(0).Value, s.Cells(1).Value, s.Cells(2).Value, s.Cells(3).Value)
DataGridView2.Sort(DataGridView2.Columns(0), System.ComponentModel.ListSortDirection.Ascending)
DataGridView1.Rows.RemoveAt(GetRowIndex(DataGridView1, s.Cells(0).Value))
End Sub
Private Sub DataGridView1_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles DataGridView1.DragDrop
Dim s As DataGridViewRow = e.Data.GetData(GetType(DataGridViewRow))
DataGridView1.Rows.Add(s.Cells(0).Value, s.Cells(1).Value, s.Cells(2).Value, s.Cells(3).Value)
DataGridView1.Sort(DataGridView2.Columns(0), System.ComponentModel.ListSortDirection.Ascending)
DataGridView2.Rows.RemoveAt(GetRowIndex(DataGridView2, s.Cells(0).Value))
End Sub
Private Sub DataGridView2_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles DataGridView2.DragEnter
e.Effect = DragDropEffects.Copy
End Sub
Private Sub DataGridView1_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles DataGridView1.DragEnter
e.Effect = DragDropEffects.Copy
End Sub
Private Function GetRowIndex(ByRef SourceGrid As DataGridView, ByVal StrSearchString As String) As Integer
Dim iRowIndex As Integer = 0
For Each Row As DataGridViewRow In SourceGrid.Rows
If SourceGrid.Rows(iRowIndex).Cells(0).Value.ToString = StrSearchString Then
Exit For
End If
iRowIndex += 1
Next Row
Return iRowIndex
End Function
End Class
Thanks
Srinivas
I am trying to design a form with two datagridviews on it and enable drag and drop between. The user should be able to drag and drop rows from grid1 to grid2 and vice versa. When a row is drag and dropped it should be removed from the source grid and added to the target grid.
One way dragdrop is happening but when trying toway method, mouse click on the grid is firing dragdrop event and creating problems. I am posting my code below please help me complete the task.
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
DataGridView1.Rows.Add(1, "Emp1", "Team Lead", "Y")
DataGridView1.Rows.Add(2, "Emp2", "Team Lead", "N")
DataGridView1.Rows.Add(3, "Emp3", "Team Lead", "Y")
DataGridView1.Rows.Add(4, "Emp4", "Team Lead", "Y")
DataGridView1.Rows.Add(5, "Emp5", "Team Lead", "N")
DataGridView1.Rows.Add(6, "Emp6", "Team Lead", "Y")
End Sub
Private Sub DataGridView1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles DataGridView1.MouseDown
If e.Button = Windows.Forms.MouseButtons.Left Then
If DataGridView1.SelectedRows.Count <> 0 Then
Dim i As Integer
For i = DataGridView1.SelectedRows.Count - 1 To 0 Step -1
If DataGridView1.SelectedRows(i).Cells("Flag").Value = "Y" Then
DataGridView1.DoDragDrop(DataGridView1.SelectedRows(i), DragDropEffects.All)
End If
Next
End If
End If
End Sub
Private Sub DataGridView2_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles DataGridView2.MouseDown
If e.Button = Windows.Forms.MouseButtons.Left Then
If DataGridView2.SelectedRows.Count <> 0 Then
Dim i As Integer
For i = DataGridView2.SelectedRows.Count - 1 To 0 Step -1
If DataGridView2.SelectedRows(i).Cells("DestFlag").Value = "Y" Then
DataGridView2.DoDragDrop(DataGridView2.SelectedRows(i), DragDropEffects.All)
End If
Next
End If
End If
End Sub
Private Sub DataGridView2_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles DataGridView2.DragDrop
Dim s As DataGridViewRow = e.Data.GetData(GetType(DataGridViewRow))
DataGridView2.Rows.Add(s.Cells(0).Value, s.Cells(1).Value, s.Cells(2).Value, s.Cells(3).Value)
DataGridView2.Sort(DataGridView2.Columns(0), System.ComponentModel.ListSortDirection.Ascending)
DataGridView1.Rows.RemoveAt(GetRowIndex(DataGridView1, s.Cells(0).Value))
End Sub
Private Sub DataGridView1_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles DataGridView1.DragDrop
Dim s As DataGridViewRow = e.Data.GetData(GetType(DataGridViewRow))
DataGridView1.Rows.Add(s.Cells(0).Value, s.Cells(1).Value, s.Cells(2).Value, s.Cells(3).Value)
DataGridView1.Sort(DataGridView2.Columns(0), System.ComponentModel.ListSortDirection.Ascending)
DataGridView2.Rows.RemoveAt(GetRowIndex(DataGridView2, s.Cells(0).Value))
End Sub
Private Sub DataGridView2_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles DataGridView2.DragEnter
e.Effect = DragDropEffects.Copy
End Sub
Private Sub DataGridView1_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles DataGridView1.DragEnter
e.Effect = DragDropEffects.Copy
End Sub
Private Function GetRowIndex(ByRef SourceGrid As DataGridView, ByVal StrSearchString As String) As Integer
Dim iRowIndex As Integer = 0
For Each Row As DataGridViewRow In SourceGrid.Rows
If SourceGrid.Rows(iRowIndex).Cells(0).Value.ToString = StrSearchString Then
Exit For
End If
iRowIndex += 1
Next Row
Return iRowIndex
End Function
End Class
Thanks
Srinivas