C# DataGridView“;索引-1没有值“1”;

C# DataGridView“;索引-1没有值“1”;,c#,winforms,datagridview,C#,Winforms,Datagridview,我在VS 2013中创建的WindowsForms应用程序中遇到DataGridView问题。 应用程序在调试生成中不会出现异常,但当我切换到发布生成,并尝试单击datagridview单元格时,出现异常: 异常:抛出:“索引-1没有值。”(System.IndexOutOfRangeException) 抛出System.IndexOutOfRangeException:“索引-1没有值。” 时间:2015-02-2819:19:29 线程:[13944] 堆栈跟踪: at System

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

异常:抛出:“索引-1没有值。”(System.IndexOutOfRangeException) 抛出System.IndexOutOfRangeException:“索引-1没有值。” 时间:2015-02-2819:19:29 线程:[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();
    }
}
更重要的是,在那个应用程序中,我有第二个表单,代码非常相似,而且运行良好。 我找到了类似问题的答案,但对我没有帮助

[编辑]


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

您如何切换到“发布”状态?如果它在debug中工作,但在release中不工作,那么当您更改为release时,必须有一些不同的东西。以目标CPU为例。

这是一个对象实例和初始化的问题


据我所知,编译器会创建System.Diagnostics.DebuggableAttribute的实例。在调试模式下,IsJitOptimizerEnabled属性为True,在发布模式下设置为False。

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

完整堆栈跟踪(或至少“哪行”部分)请..在发布版本中,我找不到堆栈跟踪,以及Application.Run(new frmBooks())上的异常ocurs,它运行所有程序。附加调试器。它在“发布”中仍然有效。如果这是您得到的唯一消息,那么某些东西正在吞咽异常并仅显示该部分。在两种构建模式中都有相同的配置。