DataGridView绑定list对象,出现索引 0 没有值

已成功测试,用bindinglist而且不用重新绑定,好用!

在我做项目的时候,datagridview绑定对象列表后,再点击datagridview控件,就会报一个"索引 -1 没有值。"的错误信息,跟踪错误信息,发现是在System.Windows.Forms.CurrencyManager.get_Item(Int32 index)里出的错,在网上搜了一下,找到了以下文章。最后,我的解决办法是,在绑定数据之前,检查对象列表是否为0,如果是,则添一个空对象到对象列表,问题解决。

我是这样绑定DataGridVeiw的:

DataGridView绑定list对象,出现索引 -1 没有值。 - kogu - kogu的博客IList<Resource> resources = new List<Resource>();
DataGridView绑定list对象,出现索引 -1 没有值。 - kogu - kogu的博客Resource  resource 
= new Resource();
DataGridView绑定list对象,出现索引 -1 没有值。 - kogu - kogu的博客resources.Add(resource);
DataGridView绑定list对象,出现索引 -1 没有值。 - kogu - kogu的博客dataGridView.DataSource 
= resources;

    
        在第一个模块中能正常使用,没有任何问题。但到了第二个模块,同样的语句,只要一点击DataGridView控件,马上就会跳出System.IndexOutOfRangeException异常,具体内容如下:

DataGridView绑定list对象,出现索引 -1 没有值。 - kogu - kogu的博客未处理 System.IndexOutOfRangeException
DataGridView绑定list对象,出现索引 -1 没有值。 - kogu - kogu的博客  Message
="索引 -1 没有值。"
DataGridView绑定list对象,出现索引 -1 没有值。 - kogu - kogu的博客  Source="System.Windows.Forms"
DataGridView绑定list对象,出现索引 -1 没有值。 - kogu - kogu的博客  StackTrace:
DataGridView绑定list对象,出现索引 -1 没有值。 - kogu - kogu的博客       在 System.Windows.Forms.CurrencyManager.get_Item(Int32 index)
DataGridView绑定list对象,出现索引 -1 没有值。 - kogu - kogu的博客       在 System.Windows.Forms.CurrencyManager.get_Current()
DataGridView绑定list对象,出现索引 -1 没有值。 - kogu - kogu的博客       在 System.Windows.Forms.DataGridView.DataGridViewDataConnection.OnRowEnter(DataGridViewCellEventArgs e)
DataGridView绑定list对象,出现索引 -1 没有值。 - kogu - kogu的博客       在 System.Windows.Forms.DataGridView.OnRowEnter(DataGridViewCell
& dataGridViewCell, Int32 columnIndex, Int32 rowIndex, Boolean canCreateNewRow, Boolean validationFailureOccurred)
DataGridView绑定list对象,出现索引 -1 没有值。 - kogu - kogu的博客       在 System.Windows.Forms.DataGridView.SetCurrentCellAddressCore(Int32 columnIndex, Int32 rowIndex, Boolean setAnchorCellAddress, Boolean validateCurrentCell, Boolean throughMouseClick)
DataGridView绑定list对象,出现索引 -1 没有值。 - kogu - kogu的博客       在 System.Windows.Forms.DataGridView.OnCellMouseDown(HitTestInfo hti, Boolean isShiftDown, Boolean isControlDown)
DataGridView绑定list对象,出现索引 -1 没有值。 - kogu - kogu的博客       在 System.Windows.Forms.DataGridView.OnCellMouseDown(DataGridViewCellMouseEventArgs e)
DataGridView绑定list对象,出现索引 -1 没有值。 - kogu - kogu的博客       在 System.Windows.Forms.DataGridView.OnMouseDown(MouseEventArgs e)
DataGridView绑定list对象,出现索引 -1 没有值。 - kogu - kogu的博客       在 System.Windows.Forms.Control.WmMouseDown(Message
& m, MouseButtons button, Int32 clicks)
DataGridView绑定list对象,出现索引 -1 没有值。 - kogu - kogu的博客       在 System.Windows.Forms.Control.WndProc(Message
& m)
DataGridView绑定list对象,出现索引 -1 没有值。 - kogu - kogu的博客       在 System.Windows.Forms.DataGridView.WndProc(Message
& m)
DataGridView绑定list对象,出现索引 -1 没有值。 - kogu - kogu的博客       在 System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message
& m)
DataGridView绑定list对象,出现索引 -1 没有值。 - kogu - kogu的博客       在 System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message
& m)
DataGridView绑定list对象,出现索引 -1 没有值。 - kogu - kogu的博客       在 System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
DataGridView绑定list对象,出现索引 -1 没有值。 - kogu - kogu的博客       在 System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG
& msg)
DataGridView绑定list对象,出现索引 -1 没有值。 - kogu - kogu的博客       在 System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
DataGridView绑定list对象,出现索引 -1 没有值。 - kogu - kogu的博客       在 System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
DataGridView绑定list对象,出现索引 -1 没有值。 - kogu - kogu的博客       在 System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
DataGridView绑定list对象,出现索引 -1 没有值。 - kogu - kogu的博客       在 System.Windows.Forms.Application.Run(Form mainForm)
DataGridView绑定list对象,出现索引 -1 没有值。 - kogu - kogu的博客       在 KoalaStudio.ksRBAC.PrivilegeConfigTool.Program.Main()
DataGridView绑定list对象,出现索引 -1 没有值。 - kogu - kogu的博客       在 System.AppDomain.nExecuteAssembly(Assembly assembly, String[] args)
DataGridView绑定list对象,出现索引 -1 没有值。 - kogu - kogu的博客       在 System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
DataGridView绑定list对象,出现索引 -1 没有值。 - kogu - kogu的博客       在 Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
DataGridView绑定list对象,出现索引 -1 没有值。 - kogu - kogu的博客       在 System.Threading.ThreadHelper.ThreadStart_Context(Object state)
DataGridView绑定list对象,出现索引 -1 没有值。 - kogu - kogu的博客       在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
DataGridView绑定list对象,出现索引 -1 没有值。 - kogu - kogu的博客       在 System.Threading.ThreadHelper.ThreadStart()
DataGridView绑定list对象,出现索引 -1 没有值。 - kogu - kogu的博客


        而且比较可恶的是,抛出异常的地方竟然是在Program.cs里,指向

DataGridView绑定list对象,出现索引 -1 没有值。 - kogu - kogu的博客Application.Run(new FrmMain());

        这让人怎么调试,试了N回,也没发现原因。

        于是上网找了找,竟然真有解决的方法,其作者也并未解决这个问题,而是采取了折衷的方法,用了一个过渡组件来解决:

DataGridView绑定list对象,出现索引 -1 没有值。 - kogu - kogu的博客BindingSource bindingSource = new BindingSource();
DataGridView绑定list对象,出现索引 -1 没有值。 - kogu - kogu的博客bindingSource.DataSource 
= resources;
DataGridView绑定list对象,出现索引 -1 没有值。 - kogu - kogu的博客dgvResource.DataSource 
= bindingSource;


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

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

猜你喜欢

转载自blog.csdn.net/jasonhongcn/article/details/84348415