DataGridView中的错误 - simpel数据源,List <generic>?</generic>

时间:2012-01-12 09:20:54

标签: c# .net winforms datagridview

如果我运行此代码,应用程序将崩溃,我不明白为什么, 它让我觉得它是一个错误 - 或者是它?

步骤:在表单上放置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没有值。     来源= 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&amp; dataGridViewCell,Int32 columnIndex,Int32 rowIndex,Boolean canCreateNewRow,Boolean validationFailureOccurred)          在System.Windows.Forms.DataGridView.SetCurrentCellAddressCore(Int32 columnIndex,Int32 rowIndex,Boolean setAnchorCellAddress,Boolean validateCurrentCell,Boolean throughMouseClick)          在System.Windows.Forms.DataGridView.OnCellMouseDown(HitTestInfo hti,Boolean isShiftDown,Boolean isControlDown)          在System.Windows.Forms.DataGridView.OnCellMouseDown(DataGridViewCellMouseEventArgs e)          在System.Windows.Forms.DataGridView.OnMouseDown(MouseEventArgs e)          在System.Windows.Forms.Control.WmMouseDown(消息&amp; m,MouseButtons按钮,Int32点击)          在System.Windows.Forms.Control.WndProc(消息&amp; m)          在System.Windows.Forms.DataGridView.WndProc(消息&amp; m)          在System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message&amp; m)          在System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message&amp; m)          在System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd,Int32 msg,IntPtr wparam,IntPtr lparam)          在System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG&amp; 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 reason,ApplicationContext context)          在System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason,ApplicationContext context)          在System.Windows.Forms.Application.Run(Form mainForm)          at WindowsFormsApplication3.Program.Main()in C:\ TestUtveckling \ WindowsFormsApplication3 \ WindowsFormsApplication3 \ Program.cs:第18行          在System.AppDomain._nExecuteAssembly(RuntimeAssembly程序集,String [] args)          在System.AppDomain.ExecuteAssembly(String assemblyFile,Evidence assemblySecurity,String [] args)          在Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()          在System.Threading.ThreadHelper.ThreadStart_Context(对象状态)          at System.Threading.ExecutionContext.Run(ExecutionContext executionContext,ContextCallback callback,Object state,Boolean ignoreSyncCtx)          在System.Threading.ExecutionContext.Run(ExecutionContext executionContext,ContextCallback回调,对象状态)          在System.Threading.ThreadHelper.ThreadStart()     InnerException:

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

1 个答案:

答案 0 :(得分:1)

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

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