Windows 窗体应用程序异常 [英] Windows form application exception

查看:25
本文介绍了Windows 窗体应用程序异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到应用程序异常

   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 MedForms.Program.Main() in F:ProjectsVstuMedFormsMedFormsProgram.cs:line 18
   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.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Threading.ThreadHelper.ThreadStart()

每次我尝试点击 DataGridView 时.

each time when I try to click on DataGridView.

我收到错误信息

{"索引 -1 没有值."}(SystemIndexOutOfaRange 异常).

{"Index -1 does not have a value."} (SystemIndexOutOfaRange exception).

在行

Application.Run(new MainForm());

我无法调试它.请帮我找出导致此类问题的原因以及如何调试?

and I am not able to debug it. Please help me find what can cause such problem and how I can to debug it?

推荐答案

我猜你已经将一个最初为空的 List(或其他类型的不生成列表更改事件的集合)绑定到你的 DataGridView,然后将项目添加到此列表中.

I'm guessing that you have bound a List that is initially empty, (or other sort of collection that does not generate list changed events) to your DataGridView, and then added items to this List.

您添加的项目将显示在您的网格上正确,但单击一行会导致此异常.这是因为底层 CurrencyManager 会将其当前行位置报告为 -1 的偏移量.它将保持这种状态,因为 List 不会向网格报告更改.

The items you add will display correctly on your grid, but clicking on a row will cause this exception. This is because the underlying CurrencyManager will be reporting its current row position as an offset of -1. It will stay this way because the List does not report changes to the grid.

您应该只将列表绑定到网格,如果它开始有一些项目,或者在添加它们时重新绑定.

You should only bind your list to the grid if it has some items in it to begin with, or rebind when you add them.

另见我对this 问题,本质上是同一个问题.

See also my answer to this question, which is essentially the same problem.

这篇关于Windows 窗体应用程序异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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