469,962 Members | 2,353 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,962 developers. It's quick & easy.

Drag and Drop Unhandled NullReferenceException

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.Proj ectRegister)
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.Proj ectRegister)
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.IDat aErrorInfo.get_Item(String
colName)
at
System.Windows.Forms.DataGridView.DataGridViewData Connection.GetError(Int32
boundColumnIndex, Int32 columnIndex, Int32 rowIndex)
at System.Windows.Forms.DataGridViewCell.GetErrorText (Int32
rowIndex)
at
System.Windows.Forms.DataGridViewTextBoxCell.GetEr rorIconBounds(Graphics
graphics, DataGridViewCellStyle cellStyle, Int32 rowIndex)
at
System.Windows.Forms.DataGridViewCell.GetErrorIcon Bounds(Int32
rowIndex)
at
System.Windows.Forms.DataGridViewCell.UpdateCurren tMouseLocation(DataGridViewCellMouseEventArgs
e)
at
System.Windows.Forms.DataGridViewCell.OnMouseMoveI nternal(DataGridViewCellMouseEventArgs
e)
at
System.Windows.Forms.DataGridView.OnCellMouseMove( DataGridViewCellMouseEventArgs
e)
at
System.Windows.Forms.DataGridView.UpdateMouseEnter edCell(HitTestInfo
hti, MouseEventArgs e)
at System.Windows.Forms.DataGridView.OnMouseMove(Mous eEventArgs
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.O nMessage(Message& m)
at
System.Windows.Forms.Control.ControlNativeWindow.W ndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallba ck(IntPtr
hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at
System.Windows.Forms.UnsafeNativeMethods.DispatchM essageW(MSG& msg)
at
System.Windows.Forms.Application.ComponentManager. System.Windows.Forms.UnsafeNativeMethods.IMsoCompo nentManager.FPushMessageLoop(Int32
dwComponentID, Int32 reason, Int32 pvLoopData)
at
System.Windows.Forms.Application.ThreadContext.Run MessageLoopInner(Int32
reason, ApplicationContext context)
at
System.Windows.Forms.Application.ThreadContext.Run MessageLoop(Int32
reason, ApplicationContext context)
at System.Windows.Forms.Application.Run(ApplicationCo ntext
context)
at
Microsoft.VisualBasic.ApplicationServices.WindowsF ormsApplicationBase.OnRun()
at
Microsoft.VisualBasic.ApplicationServices.WindowsF ormsApplicationBase.DoApplicationModel()
at
Microsoft.VisualBasic.ApplicationServices.WindowsF ormsApplicationBase.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.Run UsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context( Object
state)
at System.Threading.ExecutionContext.Run(ExecutionCon text
executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()

Apr 9 '06 #1
0 1590

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Lauren Quantrell | last post: by
2 posts views Thread by SamSpade | last post: by
3 posts views Thread by Ajay Krishnan Thampi | last post: by
2 posts views Thread by Dante | last post: by
7 posts views Thread by Chuck Hartman | last post: by
6 posts views Thread by jojobar | last post: by
3 posts views Thread by VB Programmer | last post: by
1 post views Thread by rainxy | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.