Solved

index out of range deleting last row of datagridview

Posted on 2007-11-27
3
2,160 Views
Last Modified: 2013-11-26
Only when deleting the last row do I get an error

indexoutofrange exception was unhandled
Index # is either negative or above rows count.

this my code it happens after this fnishes so I cant find the error
    Private Sub dgv_UserDeletingRow(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewRowCancelEventArgs) Handles dgv.UserDeletingRow
        MyTableAdapter.Delete(dgv(dgvceID.Index, e.Row.Index).Value)
        MyTableAdapter.Fill(MyDataSet.Employee)
    End Sub



this is the stack trace

   at System.Data.DataView.GetElement(Int32 index)
   at System.Data.DataView.System.Collections.IList.get_Item(Int32 recordIndex)
   at System.Windows.Forms.BindingSource.get_Item(Int32 index)
   at System.Windows.Forms.BindingSource.RemoveAt(Int32 index)
   at System.Windows.Forms.CurrencyManager.RemoveAt(Int32 index)
   at System.Windows.Forms.DataGridView.DataGridViewDataConnection.DeleteRow(Int32 rowIndex)
   at System.Windows.Forms.DataGridView.ProcessDeleteKey(Keys keyData)
   at System.Windows.Forms.DataGridView.ProcessDataGridViewKey(KeyEventArgs e)
   at System.Windows.Forms.DataGridView.OnKeyDown(KeyEventArgs e)
   at System.Windows.Forms.Control.ProcessKeyEventArgs(Message& m)
   at System.Windows.Forms.DataGridView.ProcessKeyEventArgs(Message& m)
   at System.Windows.Forms.Control.ProcessKeyMessage(Message& m)
   at System.Windows.Forms.Control.WmKeyChar(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 MyProgram.My.MyApplication.Main(String[] Args) in 17d14f5c-a337-4978-8281-53493378c1071.vb:line 81
   at System.AppDomain.nExecuteAssembly(Assembly assembly, String[] args)
   at System.Runtime.Hosting.ManifestRunner.Run(Boolean checkAptModel)
   at System.Runtime.Hosting.ManifestRunner.ExecuteAsAssembly()
   at System.Runtime.Hosting.ApplicationActivator.CreateInstance(ActivationContext activationContext, String[] activationCustomData)
   at System.Runtime.Hosting.ApplicationActivator.CreateInstance(ActivationContext activationContext)
   at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssemblyDebugInZone()
   at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Threading.ThreadHelper.ThreadStart()


any ideas?
0
Comment
Question by:DangerMode
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 16

Accepted Solution

by:
gnoon earned 500 total points
ID: 20362581
I think both MyTableAdapter and dgv are associated together. If one was updated, another is updated too.

While deleting from dgv, you've re-filled the MyTableAdapter. At this time, the dgv was also updated with other data (row number is not the same). But the deleting command on dgv is not break out. It continues to delete the last row which was deleted by command MyTableAdapter.Delete.

Try to add command

e.Cancel = True

at the end of Sub.
0
 

Author Comment

by:DangerMode
ID: 20362641
You are a genius.
I didnt understand a word of that, but it worked thank you so much.
0
 
LVL 16

Expert Comment

by:gnoon
ID: 20362773
What I said is "you're doing job which was already done".

The code you're showing having DELETE operation two times.
1st  - MyTableAdapter.Delete
2nd - dgv_UserDeletingRow which is going to delete too

You just need to do is stoping the 2nd to go to delete the row (again).

However, I'm glad it can help you :-)
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question