Solved

index out of range deleting last row of datagridview

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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Form design in vb.net 7 20
Change data in datatable 8 18
Create XML 5 34
Vb.Net Date Formatting Assistance 4 16
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…
This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

759 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now