DataGridView“索引-1没有值” [英] DataGridView "index -1 does not have a value"

查看:129
本文介绍了DataGridView“索引-1没有值”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在VS 2013中创建的WindowsForms应用程序中的DataGridView出现问题。
应用程序在Debug构建中正常工作,但是当我切换到Release build并尝试单击datagridview单元时,我有一个例外:

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:


异常:抛出:索引-1没有值。 (System.IndexOutOfRangeException)
引发了System.IndexOutOfRangeException:索引-1没有值。
时间:2015-02-28 19:19:29
线程:[13944]

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]

堆栈跟踪:

   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.

我意识到两种构建模式都发生异常,但是要发生这种情况需要特定的情况。不久,我的程序将存储有关书籍的数据(保存在xml文件中),现在在创建列表并添加第一个元素时发生异常(然后在尝试单击DGV时发生),但是当我保存XMl和然后再次阅读它,我可以毫无例外地对其进行操作。

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.

推荐答案

尝试使用 BindingList(of T) 而不是 List(of T),并将其用作DataGridView的数据源。为了在Linq中使用,您每次都必须使用 .ToList()方法将其转换为列表。希望能帮助到你! ;)

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! ;)

这篇关于DataGridView“索引-1没有值”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆