Wednesday, November 11, 2009

Datagridview CellDoubleClick event not getting fires with Datagridview drag drop functionality in VB.NET Window form

Differentiate between DataGridView Mousedown and CellDoubleclick events

OR

Datagridview CellDoubleClick event not getting fires with Datagridview drag drop functionality in VB.NET Window form


Problem


In VB.NET with Visual Studio 2005 I face a problem with working Datagridview drag and drop functionality. When I write a code to implement drag and drop functionality in Datagridview MouseDown event

Ex: DataGridView1.DoDragDrop(Index, DragDropEffects.Move)


And trying to fire CellDoubleClick event for same datagrid then this event is not getting fire, problem is when you do double click then first MouseDown event will fire then CelldoubleClick, but if control goes to MouseDown event then this line of code "DataGridView1.DoDragDrop(Index, DragDropEffects.Move)" canel CellDoubleClick event and CellDoubleClick event not getting fire.


Solution


To work with Both events togather we have to differenciate this by "Clicks" property in MouseDown event

Ex:

Private Sub DataGridView1_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles DataGridView1.MouseDown


' 'Clicks' property can be used to solve this issue

If e.Clicks = 1 Then

' Write Code for Single Click

Else

'Write code for Double Click

End If

End Sub


Source code


So finally cell double and drag drop code should be like below


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

If e.Button = Windows.Forms.MouseButtons.Left Then

Index = DataGridView1.HitTest(e.X, e.Y).RowIndex

If e.Clicks = 1 Then ' For Single Click

If Index > -1 Then

'Pass the Index as "Data" argument of the DoDragDrop 'Function,

'Instead of Index as "Data" we can pass some data like 'array, object or images,

'That can be collected DataGridView2_DragOve event like

'e.Data.GetData(GetType(System.String))

DataGridView1.DoDragDrop(Index, DragDropEffects.Move)

End If

End If

End If

End Sub


Private Sub DataGridView1_CellDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellDoubleClick

MessageBox.Show("DGV1_CellDoubleClick")

End Sub


Private Sub DataGridView2_DragOver(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles DataGridView2.DragOver

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

'Write your code do operation in DataGridView2

End Sub

How to implements Drag Drop functionality with DataGridView : click here

2 comments:

Anonymous said...

Dear Author riteshk.blogspot.com !
I am final, I am sorry, I too would like to express the opinion.

Anonymous said...

I want to quote your post in my blog. It can?
And you et an account on Twitter?