I have successfully implemented drag and drop in my application to
allow the reordering of columns by dragging and dropping them in the
same datagridvire (Net 2.0).
If i take it relatively slowly this works perfectly. However, if I
perform consecutive drag and drops quickly, the code seems to trip over
itself and throws the exception or other similar ones as per below.
The main problem is that no matter where I try to catch this exception,
I cannot. Not even in the application level unhandled exceptions event.
I have try and catch wrappers around every bit of code.
Can someone please advise;
1) how to catch this error
2) how to stop the drag and drop code falling over itself without using
crudemethods like timers.
Cheers
Dennis
This is the D&D code, though I don't think its actually relevant as it
works fine until done quickly.
Private Sub dgProjects_Mous eDown(ByVal sender As System.Object, ByVal
e As System.Windows. Forms.MouseEven tArgs) Handles dgProjects.Mous eDown
Dim hit As DataGridView.Hi tTestInfo = dgProjects.HitT est(e.X,
e.Y)
Dim dragSize As Size = SystemInformati on.DragSize
Dim pointTopLeft As Point = New Point(e.X - (dragSize.Width /
2), e.Y - (dragSize.Heigh t / 2))
dragRow = hit.RowIndex
dragBoxFromMous eDown = New Rectangle(point TopLeft, dragSize)
End Sub
Private Sub dgProjects_Mous eMove(ByVal sender As Object, ByVal e As
System.Windows. Forms.MouseEven tArgs) Handles dgProjects.Mous eMove
Try
If e.Button = Windows.Forms.M ouseButtons.Lef t Then
If Not dragBoxFromMous eDown.Contains( e.X, e.Y) Then
dgProjects.DoDr agDrop(dgProjec ts.Rows(dragRow ),
DragDropEffects .Move)
End If
End If
Catch
End Try
End Sub
Private Sub dgProjects_Mous eUp(ByVal sender As Object, ByVal e As
System.Windows. Forms.MouseEven tArgs) Handles dgProjects.Mous eUp
Try
dragBoxFromMous eDown = Rectangle.Empty
Catch
End Try
End Sub
Private Sub dgProjects_Drag Enter(ByVal sender As Object, ByVal e As
System.Windows. Forms.DragEvent Args) Handles dgProjects.Drag Enter
Try
e.Effect = DragDropEffects .Move
Catch
End Try
End Sub
Private Sub dgProjects_Drag Drop(ByVal sender As Object, ByVal e As
System.Windows. Forms.DragEvent Args) Handles dgProjects.Drag Drop
Try
Dim clientPoint As Point = dgProjects.Poin tToClient(New
Point(e.X, e.Y))
Dim hit As DataGridView.Hi tTestInfo =
dgProjects.HitT est(clientPoint .X, clientPoint.Y)
Dim myType As Type = GetType(DataGri dViewRow)
If e.Data.GetData( myType).Index = hit.RowIndex Then Exit
Sub 'Dropped on self
Dim DestRowOrderID As Integer = dgProjects("Ord erID",
hit.RowIndex).V alue
Dim movedRow As DataGridViewRow =
dgProjects.Rows (e.Data.GetData (myType).Index)
Dim DestRowAboveOrd erID As Integer
Dim OrderGap As Integer
If hit.RowIndex = 0 Then 'Dropped at top
DestRowAboveOrd erID = 0
movedRow.Cells( "OrderID").Valu e = DestRowOrderID + 20
OrderGap = 5
Else 'Dropped
DestRowAboveOrd erID = dgProjects("Ord erID",
hit.RowIndex - 1).Value
OrderGap = DestRowAboveOrd erID - DestRowOrderID
movedRow.Cells( "OrderID").Valu e = DestRowOrderID +
Int(OrderGap / 2)
End If
dgProjects.EndE dit()
Me.Validate()
ProjectRegister BindingSource.S ort = "Active DESC, OrderID
DESC"
Try
ProjectRegister TableAdapter.Up date(dsProjects .ProjectRegiste r)
dsProjects.Acce ptChanges()
Catch ex As Exception
End Try
If OrderGap <= 2 Then
For Each dgvr As DataGridViewRow In dgProjects.Rows
dgvr.Cells("Ord erID").Value = dgvr.Index * 20
Next
ProjectRegister BindingSource.S ort = "Active DESC,
OrderID DESC"
Try
ProjectRegister TableAdapter.Up date(dsProjects .ProjectRegiste r)
dsProjects.Acce ptChanges()
Catch ex As Exception
End Try
End If
Catch
End Try
End Sub
*************** *********
Here is the error code
*************** ********
System.NullRefe renceException was unhandled
Message="Object reference not set to an instance of an object."
Source="System. Data"
StackTrace:
at
System.Data.Dat aRowView.System .ComponentModel .IDataErrorInfo .get_Item(Strin g
colName)
at
System.Windows. Forms.DataGridV iew.DataGridVie wDataConnection .GetError(Int32
boundColumnInde x, Int32 columnIndex, Int32 rowIndex)
at System.Windows. Forms.DataGridV iewCell.GetErro rText(Int32
rowIndex)
at
System.Windows. Forms.DataGridV iewTextBoxCell. GetErrorIconBou nds(Graphics
graphics, DataGridViewCel lStyle cellStyle, Int32 rowIndex)
at
System.Windows. Forms.DataGridV iewCell.GetErro rIconBounds(Int 32
rowIndex)
at
System.Windows. Forms.DataGridV iewCell.UpdateC urrentMouseLoca tion(DataGridVi ewCellMouseEven tArgs
e)
at
System.Windows. Forms.DataGridV iewCell.OnMouse MoveInternal(Da taGridViewCellM ouseEventArgs
e)
at
System.Windows. Forms.DataGridV iew.OnCellMouse Move(DataGridVi ewCellMouseEven tArgs
e)
at
System.Windows. Forms.DataGridV iew.UpdateMouse EnteredCell(Hit TestInfo
hti, MouseEventArgs e)
at System.Windows. Forms.DataGridV iew.OnMouseMove (MouseEventArgs
e)
at System.Windows. Forms.Control.W mMouseMove(Mess age& m)
at System.Windows. Forms.Control.W ndProc(Message& m)
at System.Windows. Forms.DataGridV iew.WndProc(Mes sage& m)
at
System.Windows. Forms.Control.C ontrolNativeWin dow.OnMessage(M essage& m)
at
System.Windows. Forms.Control.C ontrolNativeWin dow.WndProc(Mes sage& m)
at System.Windows. Forms.NativeWin dow.DebuggableC allback(IntPtr
hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at
System.Windows. Forms.UnsafeNat iveMethods.Disp atchMessageW(MS G& msg)
at
System.Windows. Forms.Applicati on.ComponentMan ager.System.Win dows.Forms.Unsa feNativeMethods .IMsoComponentM anager.FPushMes sageLoop(Int32
dwComponentID, Int32 reason, Int32 pvLoopData)
at
System.Windows. Forms.Applicati on.ThreadContex t.RunMessageLoo pInner(Int32
reason, ApplicationCont ext context)
at
System.Windows. Forms.Applicati on.ThreadContex t.RunMessageLoo p(Int32
reason, ApplicationCont ext context)
at System.Windows. Forms.Applicati on.Run(Applicat ionContext
context)
at
Microsoft.Visua lBasic.Applicat ionServices.Win dowsFormsApplic ationBase.OnRun ()
at
Microsoft.Visua lBasic.Applicat ionServices.Win dowsFormsApplic ationBase.DoApp licationModel()
at
Microsoft.Visua lBasic.Applicat ionServices.Win dowsFormsApplic ationBase.Run(S tring[]
commandLine)
at Packageer.My.My Application.Mai n(String[] Args) in
17d14f5c-a337-4978-8281-53493378c1071.v b:line 81
at System.AppDomai n.nExecuteAssem bly(Assembly assembly, String[]
args)
at System.AppDomai n.ExecuteAssemb ly(String assemblyFile,
Evidence assemblySecurit y, String[] args)
at
Microsoft.Visua lStudio.Hosting Process.HostPro c.RunUsersAssem bly()
at System.Threadin g.ThreadHelper. ThreadStart_Con text(Object
state)
at System.Threadin g.ExecutionCont ext.Run(Executi onContext
executionContex t, ContextCallback callback, Object state)
at System.Threadin g.ThreadHelper. ThreadStart()