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:
在VS 2013创建的windows窗体应用程序中,我遇到了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]
异常:抛出:“Index -1没有值。”(System.IndexOutOfRangeException)系统。IndexOutOfRangeException被抛出:“Index -1没有值。”时间:2015-02-28 19:19:29线程:[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.
我意识到这个异常在构建模式中发生,但是在发生时它需要一个特定的情况。不久我的程序将数据存储书籍(保存在xml文件),现在除了ocurs创建列表时,添加的第一个元素(然后试着点击DGV它发生时),但是当我保存xml然后读一遍我可以操作它,没有例外。
3
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! ;)
尝试使用BindingList(of T)而不是List(of T),并将其用作DataGridView的数据源。对于Linq的使用,您必须每次使用“. tolist()”方法将其转换为列表。希望它可以帮助!,)
0
How are you going about switching to 'release'? If it works in debug, but not release, there has to be something different when you change to release. Take a look at the target CPU for instance.
你打算如何切换到“release”?如果它在调试中工作,但是没有发布,那么当您更改发布时,必须有一些不同的地方。例如,看一下目标CPU。
0
It is a problem of objects instances and initializations.
它是对象实例和初始化的问题。
what i know about this issue that the compiler create an instance of the System.Diagnostics.DebuggableAttribute. In the debug mode, the IsJitOptimizerEnabled property is True, in the release mode it is set to False.
我对这个问题的了解是编译器创建系统的一个实例。在调试模式中,IsJitOptimizerEnabled属性为真,在发布模式下设置为False。