Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DataGridView "index -1 does not have a value"

I have a problem with DataGridView in WindowsForms Application created in VS 2013. Application works with no exceptions in Debug build, but when I switch to Release build, and try to click on datagridview cell I have an exception:

Exception:Thrown: "Index -1 does not have a value." (System.IndexOutOfRangeException) A System.IndexOutOfRangeException was thrown: "Index -1 does not have a value." Time: 2015-02-28 19:19:29 Thread:[13944]

Stack trace:

   at System.Windows.Forms.CurrencyManager.get_Item(Int32 index)
   at System.Windows.Forms.CurrencyManager.get_Current()
   at System.Windows.Forms.DataGridView.DataGridViewDataConnection.OnRowEnter(DataGridViewCellEventArgs e)
   at System.Windows.Forms.DataGridView.OnRowEnter(DataGridViewCell& dataGridViewCell, Int32 columnIndex, Int32 rowIndex, Boolean canCreateNewRow, Boolean validationFailureOccurred)
   at System.Windows.Forms.DataGridView.SetCurrentCellAddressCore(Int32 columnIndex, Int32 rowIndex, Boolean setAnchorCellAddress, Boolean validateCurrentCell, Boolean throughMouseClick)
   at System.Windows.Forms.DataGridView.OnCellMouseDown(HitTestInfo hti, Boolean isShiftDown, Boolean isControlDown)
   at System.Windows.Forms.DataGridView.OnCellMouseDown(DataGridViewCellMouseEventArgs e)
   at System.Windows.Forms.DataGridView.OnMouseDown(MouseEventArgs e)
   at System.Windows.Forms.Control.WmMouseDown(Message& m, MouseButtons button, Int32 clicks)
   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(IntPtr 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(Form mainForm)
   at Katalogowanie.Program.Main()
   at System.AppDomain._nExecuteAssembly(RuntimeAssembly 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.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Threading.ThreadHelper.ThreadStart()

Here is the code which do the stuff:

private void dgvList_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
    dgvList.Refresh();
    var senderGrid = (DataGridView)sender;

    if (e.RowIndex >= 0)
    {
        if (senderGrid.Columns[e.ColumnIndex].Name == "Delete")
        {
            Book b = (Book)dgvList.CurrentCell.OwningRow.DataBoundItem;
            ArrayOfBooks.Remove(b);
            fillGV();
        }
    }
}

private void dgvList_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
{
    dgvList.Refresh();
    var senderGrid = (DataGridView)sender;
    if (senderGrid.Columns[e.ColumnIndex].Name == "Author" && e.RowIndex >= 0)
    {
        Book b = (Book)dgvList.CurrentCell.OwningRow.DataBoundItem;
        frmAuthors frmAuth = new frmAuthors(b);
        frmAuth.mainForm = this;
        frmAuth.Show();
    }
}

What is more in that app I have second form with very similar code and it works well. I found answer for similar question(here) but it doesn't help me.

[EDIT]

I realized that this exception occurs in both build modes, but to occur it needs a specific situation. Shortly my program will store data about books( saved in xml file), and now the exception ocurs when the list is created and the first elements are added(then when try to click on DGV it occurs), but when I save the XMl and then read it again I can operate on it with no exceptions.

like image 319
Michał Świątek Avatar asked Mar 17 '23 21:03

Michał Świątek


1 Answers

Try to use a BindingList(of T) instead of List(of T) and use it as the DataSource for the DataGridView. For the use in Linq you'll have to convert it to a list each time with the ".ToList()" method. Hope it helps! ;)

like image 131
Thomas Hahn Avatar answered Mar 20 '23 05:03

Thomas Hahn