2010-12-20 4 views
7

나는 응용 프로그램 예외윈도우 폼 응용 프로그램 예외

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 값이 없습니다."} (SystemIndexOutOfaRange 예외를) 얻을.

Application.Run(new MainForm()); 

나는 그것을 디버깅 할 수 없습니다입니다. 문제를 일으킬 수있는 부분과 디버깅 방법을 찾도록 도와주세요.

+1

예외 메시지 란 무엇입니까? 가장 큰 실마리는 바로 CurrencyManager에 대한 참조입니다. 그리드가 통화로 무엇을하고 있습니까? 특히, 클릭하는 행의 데이터에 문제가 있습니까? – David

+0

David와 함께 전체 스택 추적은 무엇입니까? 이것은 중요한 정보가 누락되었습니다 – NotMe

+0

전체 스택 추적입니다. 아무것도 없다 ... 나는 오류 메시지를 추가 할 수있다. { "인덱스 -1에는 값이 없다."} (SystemIndexOutOfaRange 예외). 나는 그것을 질문에 추가했다. – Anton

답변

20

처음에는 비어있는 List (또는 목록이 변경된 이벤트를 생성하지 않는 다른 종류의 컬렉션)를 DataGridView에 바인딩 한 다음이 목록에 항목을 추가했다고 생각합니다.

항목은 당신이 올바르게 그리드 표시됩니다 추가 할 수 있지만 행을 클릭하면이 예외가 발생합니다. 기본 CurrencyManager이 -1의 오프셋으로 현재 행 위치를보고하기 때문입니다. 목록에서 변경 내용을 그리드에보고하지 않기 때문에이 방법으로 유지됩니다.

목록에 항목이있는 경우 목록을 바인딩해야하며 목록을 추가 할 때 다시 바인딩해야합니다.

내 대답은 this입니다.이 질문은 본질적으로 동일한 문제입니다. 앤디의 조언에 따라

+0

예, 바인딩 목록입니다. 바인딩 전에 목록이 비어 있는지 확인하려고합니다. – Anton

+0

감사합니다. 정말 도움이됩니다! – Anton

+1

@Anton : 나는 좋은 추측을했다 :) – Andy

0

나는

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

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

교체하고 문제가 사라졌다.

관련 문제