c# - 数据 GridView "index -1 does not have a value"

标签 c# winforms datagridview

我在 VS 2013 中创建的 WindowsForms 应用程序中遇到 DataGridView 问题。 应用程序在调试构建中没有异常,但是当我切换到发布构建并尝试单击 datagridview 单元格时,出现异常:

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()

这是执行这些操作的代码:

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();
    }
}

更重要的是,在该应用程序中,我有第二种形式,其代码非常相似,并且运行良好。 我找到了类似问题的答案(here),但这对我没有帮助。

[编辑]

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

最佳答案

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

关于c# - 数据 GridView "index -1 does not have a value",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28785307/

相关文章:

c# - SQLite C# 和 DataGridView

c# - 配置 Windows DNS 解析器缓存

c# - 我是否完全误解了如何使用 Visual Studio 的 2008 分析器?

c# - NUnit 错误 : Not a test assembly

c# - Windows 窗体 TabIndex 不遵循索引

c# - 如何在 C# 中检索 BindingNavigator.PositionItem 的值?

json - 如何在 VB.NET Newtonsoft 中解析 Json 列表

c# - ASP.NET 成员资格 - 从成员资格表中检索密码和 PasswordSalt - 哈希用户 ID

c# - 如何创建更好看的 DataGridView?

c# - C# Windows 应用程序中的打印问题 - 始终打印到默认打印机