C# DataGridView中的错误-simpel数据源,列表<;通用>;?
如果我运行此代码,应用程序将崩溃,我不明白为什么, 在我看来,这是一只虫子——还是真的 步骤:在窗体上放置System.Windows.Forms.DataGridView 定义数据源类:C# DataGridView中的错误-simpel数据源,列表<;通用>;?,c#,.net,winforms,datagridview,C#,.net,Winforms,Datagridview,如果我运行此代码,应用程序将崩溃,我不明白为什么, 在我看来,这是一只虫子——还是真的 步骤:在窗体上放置System.Windows.Forms.DataGridView 定义数据源类: public class SomeClass { public SomeClass() { col1 = string.Empty; Col2= string.Empty; } public str
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 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 !
例外情况:
大宗报价
System.IndexOutOfRangeException未处理
Message=索引-1没有值。
Source=System.Windows.Forms
堆栈跟踪:
位于System.Windows.Forms.CurrencyManager.get_项(Int32索引)
在System.Windows.Forms.CurrencyManager.get_Current()中
位于System.Windows.Forms.DataGridView.DataGridViewDataConnection.OnRowEnter(DataGridViewCellEventArgs e)
在System.Windows.Forms.DataGridView.OnRowCenter(DataGridViewCell&DataGridViewCell、Int32 columnIndex、Int32 rowIndex、Boolean canCreateNewRow、Boolean ValidationFailureOccessed)
位于System.Windows.Forms.DataGridView.SetCurrentCellAddressCore(Int32 columnIndex、Int32 rowIndex、Boolean setAnchorCellAddress、Boolean validateCurrentCell、Boolean throughMouseClick)
在System.Windows.Forms.DataGridView.OnCellMouseDown(HitTestInfo hti、Boolean IsShift Down、Boolean isControlDown)
位于System.Windows.Forms.DataGridView.OnCellMouseDown(DataGridViewCellMouseevenTargets e)
在System.Windows.Forms.DataGridView.OnMouseDown(MouseEventArgs e)中
在System.Windows.Forms.Control.WmMouseDown(Message&m、MouseButtons按钮、Int32单击)
位于System.Windows.Forms.Control.WndProc(Message&m)
位于System.Windows.Forms.DataGridView.WndProc(Message&m)
在System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message&m)中
在System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message&m)中
在System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd、Int32 msg、IntPtr wparam、IntPtr lparam)
在System.Windows.Forms.UnsafentiveMethods.DispatchMessageW(MSG&MSG)中
位于System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafentiveMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID、Int32 reason、Int32 pvLoopData)
位于System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32原因,ApplicationContext上下文)
位于System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32原因,ApplicationContext上下文)
在System.Windows.Forms.Application.Run(Form mainForm)中
在C:\TestUtveckling\WindowsFormsApplication3\WindowsFormsApplication3\Program.cs中的WindowsFormsApplication3.Program.Main()处:第18行
位于System.AppDomain.\u nExecuteAssembly(RuntimeAssembly程序集,字符串[]args)
位于System.AppDomain.ExecuteAssembly(字符串汇编文件、证据汇编安全性、字符串[]args)
在Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()上
位于System.Threading.ThreadHelper.ThreadStart\u上下文(对象状态)
在System.Threading.ExecutionContext.Run(ExecutionContext ExecutionContext,ContextCallback回调,对象状态,布尔ignoreSyncCtx)
在System.Threading.ExecutionContext.Run(ExecutionContext ExecutionContext,ContextCallback回调,对象状态)
位于System.Threading.ThreadHelper.ThreadStart()处
内部异常:
如果我不运行第一步,将0元素连接到数据源,
它起作用了。我说这是DataGridView中的一个bug!还是其他原因?为什么一直设置
数据源?通常,您应该只设置一次DataSource
,它将跟踪添加、删除、修改等更改。如果没有,请使用BindingSource
作为DataGridView
的数据源,使用列表作为BindingSource
的数据源,并在更改时调用BindingSource.RefreshBindings
并不是每个异常都是框架中bug的来源:-)为什么要一直设置DataSource
?通常,您应该只设置一次DataSource
,它将跟踪添加、删除、修改等更改。如果没有,请使用BindingSource
作为DataGridView
的数据源,使用列表作为BindingSource
的数据源,并在更改时调用BindingSource.RefreshBindings
并不是每个异常都是框架中bug的来源:-)是的,它可以工作,谢谢。但无论如何,它不应该崩溃。是的,它工作正常,谢谢你。但无论如何,它不应该崩溃。
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 !