Reputation: 139
How can I disable drag drop in other columns in datagridview? Like I want to disable drag drop in column 1 and the rest is not disabled. How to achieve this?
This is my code for drag drop:
Private CR As Integer, CC As Integer, CV As Object
Private Sub DataGridView1_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles DataGridView1.DragDrop
Dim ClientPoint As Point = DataGridView1.PointToClient(New Point(e.X, e.Y))
Dim hit As DataGridView.HitTestInfo = DataGridView1.HitTest(ClientPoint.X, ClientPoint.Y)
CR = hit.RowIndex : If CR < 0 Then Exit Sub
CC = hit.ColumnIndex : If CC < 0 Then Exit Sub
DataGridView1.Item(CC, CR).Value = CV
End Sub
Private Sub DataGridView1_DragOver(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles DataGridView1.DragOver
e.Effect = DragDropEffects.Move
End Sub
Private Sub DataGridView1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles DataGridView1.MouseDown
Dim hit As DataGridView.HitTestInfo = DataGridView1.HitTest(e.X, e.Y)
CR = hit.RowIndex : If CR < 0 Then Exit Sub
CC = hit.ColumnIndex : If CC < 0 Then Exit Sub
CV = DataGridView1.Item(CC, CR).Value : If CV Is Nothing Then Exit Sub
DataGridView1.DoDragDrop(CV, DragDropEffects.Move)
End Sub
Upvotes: 0
Views: 972
Reputation: 18320
You already have everything you need in order to do what you want, infact you have almost done it already.
The hit test information contains all the information you need. Just modify your code to check the ColumnIndex
and change the cursor (effect) accordingly:
Private Sub DataGridView1_DragOver(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles DataGridView1.DragOver
Dim ClientPoint As Point = DataGridView1.PointToClient(New Point(e.X, e.Y))
Dim hit As DataGridView.HitTestInfo = DataGridView1.HitTest(ClientPoint.X, ClientPoint.Y)
If hit.ColumnIndex <= 0 Then
'If we're at the first column (or no column) display a "stop" cursor.
e.Effect = DragDropEffects.None
Else
'For all other columns display a "move" cursor.
e.Effect = DragDropEffects.Move
End If
End Sub
Then, in both your DragDrop
event handler and the MouseDown
event handler, change this:
CC = hit.ColumnIndex : If CC < 0 Then Exit Sub
to this:
CC = hit.ColumnIndex : If CC <= 0 Then Exit Sub
If the column index is zero it means we're on the first column, thus exit the method since we don't want to be able to drag or drop items in it/from it.
Upvotes: 1