Tuesday, June 23, 2009

How to change mouse Icon when drag the items from DataGridView to another using VB.NET

How to change mouse Icon when drag the items from data grid view using VB.NET

Let say you have some data rows in data grid and you can select multiple rows from grid and you can drag selected rows into some other data grid or other controls

This time you will get default mouse icon and if you want to change the mouse icon with custom image then write following code in your data grid view events

Set DataGridView Properties at design time Allowdrop = true

Following code are tested with

1) .NET Framework 2.0

2) Visual Studio 2005

3) VB.NET Window Application

4) Window XP


'Following event will help to drag items on mouse left click button and this event will activate drag drop event of data grid view


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


Dim gridColumnIndex As Integer

'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


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

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

If gridColumnIndex > -1 Then

DataGridView1.DoDragDrop(gridColumnIndex, DragDropEffects.Move)

End If

End If

End Sub


'This event will remove default mouse icon

Private Sub DataGridView1_GiveFeedback(ByVal sender As Object, ByVal e As System.Windows.Forms.GiveFeedbackEventArgs) Handles DataGridView1.GiveFeedback

e.UseDefaultCursors = False

End Sub


'This event will change mouse icon with custom icon based on the one item selected or multiple items selections

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

e.Effect = DragDropEffects.Move

'if selected item is 1 then set desired icon, if more

the one then set multidrag icon else set default

If DataGridView1.SelectedRows.Count = 1 Then

Windows.Forms.Cursor.Current = New Cursor("C:\Icon\ImageSingleDrag.ico")

ElseIf DataGridView1.SelectedRows.Count > 1 Then

Windows.Forms.Cursor.Current = New Cursor("C:\Icon\ImageMultiDrag.ico")

Else

Windows.Forms.Cursor.Current = Cursors.Default

End If

End Sub


'This event will change mouse icon to no Drag icon if you move item to some other place where you can't drag the items

Private Sub DataGridView1_DragLeave(ByVal sender As Object, ByVal e As System.EventArgs) Handles DataGridView1.DragLeave

Windows.Forms.Cursor.Current = New Cursor("C:\Icon\ImageNoDrag.ico")

End Sub


'This event will deselect the items from data gird

Private Sub DataGridView1_CellMouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles DataGridView1.CellMouseUp

Dim index As Integer

index = e.RowIndex

If index > -1 Then

If DataGridView1.Rows(index).Selected = False Then

Me.DataGridView1.ClearSelection()

Me.DataGridView1.Rows(index).Selected = True

End If

End If

End Sub


If are dragging yout items into DataGridView2 then write your action code in following events

Private Sub DataGridView2_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles dgNegotiationList.DragDrop

'Write your code to do action for drag items

End Sub

Problem: DataGridView CellDoubleClick and Drag drop both not woking togather

Solution: Click Here

Learn more about how to Drag and Drop items from data grid view. See the following sites.

http://www.codeproject.com/KB/cpp/DataGridView_Drag-n-Drop.aspx

http://www.pcreview.co.uk/forums/thread-3430543.php













No comments: