Solved

index out of range deleting last row of datagridview

Posted on 2007-11-27
3
2,178 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

Stressed Out?

Watch some penguins on the livecam!

Question has a verified solution.

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

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

631 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