DataGridView中的错误-简单数据源,List<;通用>;

本文关键字:List gt lt 通用 数据源 错误 简单 DataGridView | 更新日期: 2023-09-27 18:20:09

如果我运行此代码,应用程序将崩溃,我不明白为什么,在我看来,这是一只虫子——还是它?

步骤:在窗体上放置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 !

例外:

区块报价未处理System.IndexOutOfRangeException消息=索引-1没有值。Source=System.Windows.FormsStackTrace:位于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列索引,Int32行索引,布尔canCreateNewRow,布尔验证失败)位于System.Windows.Forms.DataGridView.SetCurrentCellAddressCore(Int32列索引,Int32行索引,布尔值setAnchorCellAddress,布尔值validateCurrentCell,布尔值throughMouseClick)位于System.Windows.Forms.DataGridView.OnCellMouseDown(HitTestInfo hti,布尔值为ShiftDown,布尔值是ControlDown)位于System.Windows.Forms.DataGridView.OnCellMouseDown(DataGridViewCellMouseEventArgs e)位于System.Windows.Forms.DataGridView.OnMouseDown(MouseEventArgs e)在System.Windows.Forms.Control.WmMouseDown(消息&m,鼠标按钮按钮,Int32次点击)位于System.Windows.Forms.Control.WndProc(消息&m)位于System.Windows.Forms.DataGridView.WndProc(消息&m)位于System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message&m)位于System.Windows.Forms.Control.ControlNativeWindow.WndProc(消息&m)在System.Windows.Forms.NativeWindow.DebugableCallback(IntPtr hWnd、Int32 msg、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)位于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''WindowsFformsApplication3''Program.Main()中的位于System.AppDomain_nExecuteAssembly(RuntimeAssembly程序集,String[]参数)位于System.AppDomain.ExecuteAssembly(String assemblyFile,Evidence assemblySecurity,String[]args)位于Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()位于System.Threading.ThreadHelper.ThreadStart_Context(对象状态)在System.Threading.ExecutionContext.Run(ExecutionContext ExecutionContext,ContextCallback回调,对象状态,布尔ignoreSyncCtx)在System.Threading.ExecutionContext.Run(ExecutionContext ExecutionContext,ContextCallback回调,对象状态)位于System.Threading.ThreadHelper.ThreadStart()内部异常:

如果我不运行第一步,将0元素连接到数据源,它是有效的。我说这是DataGridView中的一个错误!还是别的什么?

DataGridView中的错误-简单数据源,List<;通用>;

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

并不是每个异常都是框架中错误的来源:-)