Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/287.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Windows窗体应用程序异常_C#_Sql_Winforms_Entity Framework_Datagridview - Fatal编程技术网

C# Windows窗体应用程序异常

C# Windows窗体应用程序异常,c#,sql,winforms,entity-framework,datagridview,C#,Sql,Winforms,Entity Framework,Datagridview,我得到了一个应用程序异常 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 Sy

我得到了一个应用程序异常

   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:\Projects\Vstu\MedForms\MedForms\Program.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没有值。”} (SystemIndexAutoFarange异常)

在行

Application.Run(new MainForm());

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

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

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

只有当列表中有一些项目要开始时,才应该将列表绑定到网格,或者在添加它们时重新绑定


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

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

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

private BindingList雇员{get;set;}=new BindingList();
_employeesGridView.DataSource=员工


问题消失了。

经过大量努力,我已经解决了这个问题。绑定DataGridView当数据源中有一些数据时,它可能是列表或DataTable

异常消息是什么?您现在最大的线索是对
CurrencyManager
的引用。网格对货币做了什么?具体来说,您单击的行中的数据是否有任何错误?除了David之外,什么是完整堆栈跟踪?这缺少重要信息,即完整堆栈跟踪。没有别的了。。。我可以补充一点,错误消息是{“Index-1没有值。”}(SystemIndexOutofarRange异常)。我把它添加到问题中。我打赌是“IndexOutOfRangeException”,因为CurrencyManager报告的当前行位置偏移量为-1。我不使用货币。DataGridView只有5列:1个日期、2个文本和2个按钮。是的,我是绑定列表。在绑定之前,我将尝试检查列表是否为空。如果在将数据
list
绑定到
DataGridView
之前,需要将
list
转换为
DataTable
(通过将
list
转换为
DataTable
)并将其用作
DataSource
,则可以解决此问题。对我来说,这比转换到
BindingList(of T)
要好,因为在数据绑定
DataGridView.DataSource=BindingList(of T)
之后,它在
DataGridView
中显示了一个空行,甚至
BindingList(of T)
在将列表(假设DataLst)绑定到DataGridView的数据源时也是空的,将其与Grid.DataSource=DataLst.ToList()绑定;