Csharp/C#教程:Windows表单应用程序exception分享


Windows表单应用程序exception

我得到应用程序exception

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时。

我收到错误消息

{“索引-1没有值。”}(SystemIndexOutOfaRangeexception)。

在行上

 Application.Run(new MainForm()); 

我无法调试它。 请帮我找到可能导致此类问题的原因以及如何调试它?

我猜你已经将一个最初为空的List(或其他不生成列表更改事件的集合) DataGridViewDataGridView ,然后将项添加到此List。

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

您应该只将列表绑定到网格中,如果它中包含一些项目,或者在添加它们时重新绑定。

另请参阅我对这个问题的回答, 这个问题基本上是一样的。

按照安迪的建议,我替换了

private List Employees { get; set; } = new List(); _employeesGridView.DataSource = Employees;

private BindingList Employees { get; set; } = new BindingList(); _employeesGridView.DataSource = Employees;

问题消失了。

上述就是C#学习教程:Windows表单应用程序exception分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/cdevelopment/956648.html

(0)
上一篇 2021年11月20日
下一篇 2021年11月20日

精彩推荐