DataGridView绑定Ilist对象,出现IndexOutOfRangeException错误的解决方法

        我是这样绑定DataGridVeiw的:
IList<Resource> resources = new List<Resource>();
Resource  resource 
= new Resource();
resources.Add(resource);
dataGridView.DataSource 
= resources;
    
        在第一个模块中能正常使用,没有任何问题。但到了第二个模块,同样的语句,只要一点击DataGridView控件,马上就会跳出System.IndexOutOfRangeException异常,具体内容如下:
未处理 System.IndexOutOfRangeException
  Message
="索引 -1 没有值。"
  Source
="System.Windows.Forms"
  StackTrace:
       在 System.Windows.Forms.CurrencyManager.get_Item(Int32 index)
       在 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, 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(Message
& m, MouseButtons button, Int32 clicks)
       在 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.UnsafeNativeMethods.DispatchMessageW(MSG
& msg)
       在 System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 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)
       在 KoalaStudio.ksRBAC.PrivilegeConfigTool.Program.Main()
       在 System.AppDomain.nExecuteAssembly(Assembly assembly, String[] args)
       在 System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       在 Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       在 System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       在 System.Threading.ThreadHelper.ThreadStart()

        而且比较可恶的是,抛出异常的地方竟然是在Program.cs里,指向
Application.Run(new FrmMain());
        这让人怎么调试,试了N回,也没发现原因。

        于是上网找了找,竟然真有解决的方法,其作者也并未解决这个问题,而是采取了折衷的方法,用了一个过渡组件来解决:
BindingSource bindingSource = new BindingSource();
bindingSource.DataSource 
= resources;
dgvResource.DataSource 
= bindingSource;

        问题可以解决,但又有了新的问题,因为使用了BindingSource,因此对与DataGridView的更改并不能同步到Ilist<Resource>对象上,所以还是不能用这种方法,继续研究。

         上国外的网站看了看,终于找到了问题的原因,在向DataGridView绑定Ilist<T>类型的对象是,如果对象的成员为0,那么就会出现
此问题。而且即使重新绑定DataGridView的数据源,也会继续存在此问题,解决的方法就是在向DataGridView绑定Ilist<T>对象是,要保证其中至少有一个成员,否则宁可不绑定(虽然不是最完美的解决方法)。
        其实最好的方法,是用BindingList<T>对象代替Ilist<T>对象作为DataGridView的数据源,即可彻底解决此问题,而且能实现DataGridView修改时与数据源的自动更新。但现在没有时间,只能先这样用了。
原文地址:https://www.cnblogs.com/benbenkoala/p/743147.html