如果我運行這段代碼,應用程序將崩潰,我不明白爲什麼, 它似乎給我它的錯誤 - 或者它?DataGridView中的錯誤 - simpel數據源,列表<generic>?
步驟:在窗體上放置一個System.Windows.Forms.DataGridView。
定義一個數據源類:
public class SomeClass
{
public SomeClass()
{
col1 = string.Empty;
Col2= string.Empty;
}
public string col1 {get;set;}
public string Col2 { get; set; }
}
//Declare new. (0 elements)
private List<SomeClass> _col = new List<SomeClass>();
//首先運行此
private void button1_Click(object sender, EventArgs e)
{
dataGridView1.DataSource = null;
dataGridView1.DataSource = _col;
}
//然後運行該
private void button2_Click(object sender, EventArgs e)
{
_col.Add(new SomeClass() { col1 = "Value1", Col2 = "Value2" });
dataGridView1.DataSource = null;
dataGridView1.DataSource = _col;
}
//If you now click in the grid it will crash.
//Note-if i dont set null - it will not be update, if i only run button2 several times
//it works fine !
例外:
Blockquote System.IndexOutOfRangeException未處理 Message = Index -1沒有值。 源= System.Windows.Forms的 堆棧跟蹤: 在System.Windows.Forms.CurrencyManager.get_Item(的Int32指數) 在System.Windows.Forms.CurrencyManager.get_Current() 在System.Windows.Forms.DataGridView.DataGridViewDataConnection .OnRowEnter(DataGridViewCellEventArgs E) 在System.Windows.Forms.DataGridView.OnRowEnter(的DataGridViewCell &的DataGridViewCell,的Int32 columnIndex,的Int32 rowIndex位置,布爾canCreateNewRow,布爾validationFailureOccurred) 在System.Windows.Forms.DataGridView.SetCurrentCellAddressCore(的Int32 columnIndex,的Int32 rowIndex,Boolean setAnchorCellAddress,Boolean validateCurrentCell,Boolean throughMouseClick) at System.Windows.Forms.DataGridView.OnCellMouseDown(HitTestInfo hti,Boole一個isShiftDown,布爾isControlDown) 在System.Windows.Forms.DataGridView.OnCellMouseDown(DataGridViewCellMouseEventArgs E) 在System.Windows.Forms.DataGridView.OnMouseDown(MouseEventArgs E) 在System.Windows.Forms.Control.WmMouseDown(消息&米,MouseButtons按鈕,點擊的Int32) 在System.Windows.Forms.Control.WndProc(消息&米) 在System.Windows.Forms.DataGridView.WndProc(消息&米) 在System.Windows.Forms.Control的。 ControlNativeWindow.OnMessage(Message & m) at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message & m) at System.Windows.Forms.Nativ eWindow.DebuggableCallback(IntPtr的的HWND,MSG的Int32,IntPtr的WPARAM,IntPtr的LPARAM) 在System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG & MSG) 在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原因,ApplicationContext上下文) at System.Windows.Forms.Application.Run(Form mainForm) at WindowsFormsApplication3.Program.Main()in C:\ TestUtveckling \ WindowsFormsApplication3 \ WindowsFormsApplication3 \ 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。在System.Threading.ExecutionContext.Run(ExecutionContext executionContext.Run(ExecutionContext executionContext,ContextCallback callback,Object state,Boolean ignoreSyncCtx)上的System.Threading.ThreadHelper.ThreadStart_Context(對象狀態) 上的RunUsersAssembly() ,ContextCallback回調,對象狀態) 在System.Threading.ThreadHelper.ThreadStart() 的InnerException:
如果我不運行第一步,0個元素連接到數據源, 它的工作原理。我說它在DataGridView中的錯誤!或者是別的什麼?
嗯是的,它的工作原理,謝謝你。但無論如何 - 它不應該崩潰。 – Niklas 2012-01-12 09:43:01