2012-01-12 51 views
1

如果我运行这段代码,应用程序将崩溃,我不明白为什么, 它似乎给我它的错误 - 或者它?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中的错误!或者是别的什么?

回答

1

你为什么继续设置DataSource?通常,您应该只设置DataSource一次,它会跟踪添加,删除,修改等更改。如果不是,请使用BindingSource作为DataGridView的数据源,并将该列表作为BindingSource的数据源,并在更改时调用BindingSource.RefreshBindings

并非每个例外都是框架中的错误的来源:-)

+0

嗯是的,它的工作原理,谢谢你。但无论如何 - 它不应该崩溃。 – Niklas 2012-01-12 09:43:01

相关问题