我在VS 2013中创建的WindowsForms应用程序中的DataGridView存在问题。 应用程序在Debug构建中没有异常,但是当我切换到Release构建,并尝试单击datagridview单元格时,我有一个例外:
异常:抛出:“索引-1没有值。” (System.IndexOutOfRangeException) 抛出了System.IndexOutOfRangeException:“索引-1没有值。” 时间:2015-02-28 19: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();
}
}
在该应用程序中还有更多我有第二种形式,代码非常相似,而且效果很好。 我找到了类似问题(here)的答案,但它对我没有帮助。
[编辑]
我意识到这种异常发生在两种构建模式中,但要发生它需要特定的情况。我的程序很快就会存储有关书籍的数据(保存在xml文件中),现在创建列表并添加第一个元素时会出现异常(然后当尝试单击DGV时会发生这种情况),但是当我保存XMl时然后再读一遍我可以毫无例外地对它进行操作。
答案 0 :(得分:4)
尝试使用 BindingList(T)而不是 List(of T),并将其用作DataGridView的DataSource。对于在Linq中的使用,您必须每次使用“.ToList()”方法将其转换为列表。希望能帮助到你! ;)
答案 1 :(得分:0)
如何切换到'发布'?如果它在调试中工作,但不是发布,则在更改为发布时必须有不同的内容。例如,查看目标CPU。
答案 2 :(得分:0)
这是对象实例和初始化的问题。
我对此问题的了解,编译器创建了System.Diagnostics.DebuggableAttribute的实例。在调试模式下,IsJitOptimizerEnabled属性为True,在发布模式下,它设置为False。