Drag and Drop Unhandled NullReferenceException

S

statlerw

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_MouseDown(ByVal sender As System.Object, ByVal
e As System.Windows.Forms.MouseEventArgs) Handles dgProjects.MouseDown
Dim hit As DataGridView.HitTestInfo = dgProjects.HitTest(e.X,
e.Y)
Dim dragSize As Size = SystemInformation.DragSize
Dim pointTopLeft As Point = New Point(e.X - (dragSize.Width /
2), e.Y - (dragSize.Height / 2))
dragRow = hit.RowIndex
dragBoxFromMouseDown = New Rectangle(pointTopLeft, dragSize)
End Sub

Private Sub dgProjects_MouseMove(ByVal sender As Object, ByVal e As
System.Windows.Forms.MouseEventArgs) Handles dgProjects.MouseMove
Try
If e.Button = Windows.Forms.MouseButtons.Left Then
If Not dragBoxFromMouseDown.Contains(e.X, e.Y) Then
dgProjects.DoDragDrop(dgProjects.Rows(dragRow),
DragDropEffects.Move)
End If
End If
Catch
End Try
End Sub

Private Sub dgProjects_MouseUp(ByVal sender As Object, ByVal e As
System.Windows.Forms.MouseEventArgs) Handles dgProjects.MouseUp
Try
dragBoxFromMouseDown = Rectangle.Empty
Catch
End Try
End Sub

Private Sub dgProjects_DragEnter(ByVal sender As Object, ByVal e As
System.Windows.Forms.DragEventArgs) Handles dgProjects.DragEnter
Try
e.Effect = DragDropEffects.Move
Catch
End Try
End Sub

Private Sub dgProjects_DragDrop(ByVal sender As Object, ByVal e As
System.Windows.Forms.DragEventArgs) Handles dgProjects.DragDrop
Try
Dim clientPoint As Point = dgProjects.PointToClient(New
Point(e.X, e.Y))
Dim hit As DataGridView.HitTestInfo =
dgProjects.HitTest(clientPoint.X, clientPoint.Y)
Dim myType As Type = GetType(DataGridViewRow)

If e.Data.GetData(myType).Index = hit.RowIndex Then Exit
Sub 'Dropped on self

Dim DestRowOrderID As Integer = dgProjects("OrderID",
hit.RowIndex).Value
Dim movedRow As DataGridViewRow =
dgProjects.Rows(e.Data.GetData(myType).Index)
Dim DestRowAboveOrderID As Integer
Dim OrderGap As Integer

If hit.RowIndex = 0 Then 'Dropped at top
DestRowAboveOrderID = 0
movedRow.Cells("OrderID").Value = DestRowOrderID + 20
OrderGap = 5
Else 'Dropped
DestRowAboveOrderID = dgProjects("OrderID",
hit.RowIndex - 1).Value
OrderGap = DestRowAboveOrderID - DestRowOrderID
movedRow.Cells("OrderID").Value = DestRowOrderID +
Int(OrderGap / 2)
End If

dgProjects.EndEdit()
Me.Validate()

ProjectRegisterBindingSource.Sort = "Active DESC, OrderID
DESC"
Try

ProjectRegisterTableAdapter.Update(dsProjects.ProjectRegister)
dsProjects.AcceptChanges()
Catch ex As Exception
End Try

If OrderGap <= 2 Then
For Each dgvr As DataGridViewRow In dgProjects.Rows
dgvr.Cells("OrderID").Value = dgvr.Index * 20
Next
ProjectRegisterBindingSource.Sort = "Active DESC,
OrderID DESC"
Try

ProjectRegisterTableAdapter.Update(dsProjects.ProjectRegister)
dsProjects.AcceptChanges()
Catch ex As Exception
End Try
End If
Catch
End Try
End Sub

************************
Here is the error code
***********************

System.NullReferenceException was unhandled
Message="Object reference not set to an instance of an object."
Source="System.Data"
StackTrace:
at
System.Data.DataRowView.System.ComponentModel.IDataErrorInfo.get_Item(String
colName)
at
System.Windows.Forms.DataGridView.DataGridViewDataConnection.GetError(Int32
boundColumnIndex, Int32 columnIndex, Int32 rowIndex)
at System.Windows.Forms.DataGridViewCell.GetErrorText(Int32
rowIndex)
at
System.Windows.Forms.DataGridViewTextBoxCell.GetErrorIconBounds(Graphics
graphics, DataGridViewCellStyle cellStyle, Int32 rowIndex)
at
System.Windows.Forms.DataGridViewCell.GetErrorIconBounds(Int32
rowIndex)
at
System.Windows.Forms.DataGridViewCell.UpdateCurrentMouseLocation(DataGridViewCellMouseEventArgs
e)
at
System.Windows.Forms.DataGridViewCell.OnMouseMoveInternal(DataGridViewCellMouseEventArgs
e)
at
System.Windows.Forms.DataGridView.OnCellMouseMove(DataGridViewCellMouseEventArgs
e)
at
System.Windows.Forms.DataGridView.UpdateMouseEnteredCell(HitTestInfo
hti, MouseEventArgs e)
at System.Windows.Forms.DataGridView.OnMouseMove(MouseEventArgs
e)
at System.Windows.Forms.Control.WmMouseMove(Message& m)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.DataGridView.WndProc(Message& m)
at
System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at
System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr
hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at
System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
at
System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32
dwComponentID, Int32 reason, Int32 pvLoopData)
at
System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32
reason, ApplicationContext context)
at
System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32
reason, ApplicationContext context)
at System.Windows.Forms.Application.Run(ApplicationContext
context)
at
Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun()
at
Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel()
at
Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String[]
commandLine)
at Packageer.My.MyApplication.Main(String[] Args) in
17d14f5c-a337-4978-8281-53493378c1071.vb:line 81
at System.AppDomain.nExecuteAssembly(Assembly assembly, String[]
args)
at System.AppDomain.ExecuteAssembly(String assemblyFile,
Evidence assemblySecurity, String[] args)
at
Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object
state)
at System.Threading.ExecutionContext.Run(ExecutionContext
executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top