问题
If i run this code the application will crash, and i dont understand why, it seemes to me its a bug - or is it?
Steps: place a System.Windows.Forms.DataGridView on a form.
Define a datasource class:
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>();
//First run this
private void button1_Click(object sender, EventArgs e)
{
dataGridView1.DataSource = null;
dataGridView1.DataSource = _col;
}
//Then run this
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 !
Exception:
Blockquote System.IndexOutOfRangeException was unhandled Message=Index -1 does not have a value. Source=System.Windows.Forms StackTrace: 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 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.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() InnerException:
If i dont run the first step, connecting the 0 elements to the datasource, it works. I say its a bug in the DataGridView ! Or is it something else?
回答1:
Why do you keep setting DataSource
? Usually, you should only set DataSource
once and it will keep track of changes like adding, removing, modifying. If it does not, use a BindingSource
as the data source for the DataGridView
and the list as the data source for the BindingSource
and call BindingSource.RefreshBindings
upon changes.
Not every exception is the source of a bug in the framework :-)
来源:https://stackoverflow.com/questions/8832587/bug-in-datagridview-simpel-datasource-listgeneric