2010-12-20 144 views
7

我一次,當我嘗試點擊DataGridView的時間應用程序異常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:\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() 

我遇到錯誤信息

{ 「指數-1不具有價值。」} (SystemIndexOutOfaRange除外)。

Application.Run(new MainForm()); 

行,我不能調試。請幫助我找到可能導致此類問題的原因以及如何進行調試?

+1

什麼是異常消息?你現在最大的線索是對'CurrencyManager'的引用。網格用貨幣做什麼?具體來說,您點擊的行中的數據是否有問題? – David 2010-12-20 21:34:09

+0

與David一起,什麼是完整的堆棧跟蹤?這是缺少重要信息 – NotMe 2010-12-20 21:37:05

+0

它是完整的堆棧跟蹤。沒有別的......我可以添加錯誤消息是{「Index -1沒有值。」}(SystemIndexOutOfaRange異常)。我添加了它的問題。 – Anton 2010-12-20 21:41:10

回答

20

我猜你已經綁定了一個初始爲空的列表(或其他類型的集合不會生成列表已更改的事件)到您的DataGridView,然後將項目添加到此列表中。

的項目你增加顯示正確地在網格上,但點擊一個行將導致此異常。這是因爲底層CurrencyManager將報告其當前行位置爲-1的偏移量。它將保持這種狀態,因爲列表不會向網格報告更改。

如果您的列表中包含一些項目,或者在添加項目時重新綁定,則只應將列表綁定到網格。

另請參閱我的回答this問題,這基本上是同一個問題。

+0

是的,我是綁定列表。我會嘗試在綁定前檢查列表是否爲空。 – Anton 2010-12-20 21:46:14

+0

謝謝,它確實有幫助! – Anton 2010-12-20 21:54:49

+1

@Anton:我做了一個很好的猜測:) – Andy 2010-12-20 21:56:23

0

繼安迪的意見,我取代

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

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

,問題就消失了。