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

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

我是这样绑定DataGridVeiw的:

IList<resource> resources = new List<resource>();<br><img src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif" align="top">Resource resource = new Resource();<br><img src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif" align="top">resources.Add(resource);<br><img src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif" align="top">dataGridView.DataSource = resources; <p> 在第一个模块中能正常使用,没有任何问题。但到了第二个模块,同样的语句,只要一点击DataGridView控件,马上就会跳出System.IndexOutOfRangeException异常,具体内容如下: </p> <p><img src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif" align="top">未处理 System.IndexOutOfRangeException<br><img src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif" align="top"> Message="索引 -1 没有值。"<br><img src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif" align="top"> Source="System.Windows.Forms"<br><img src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif" align="top"> StackTrace:<br><img src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif" align="top"> 在 System.Windows.Forms.CurrencyManager.get_Item(Int32 index)<br><img src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif" align="top"> 在 System.Windows.Forms.CurrencyManager.get_Current()<br><img src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif" align="top"> 在 System.Windows.Forms.DataGridView.DataGridViewDataConnection.OnRowEnter(DataGridViewCellEventArgs e)<br><img src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif" align="top"> 在 System.Windows.Forms.DataGridView.OnRowEnter(DataGridViewCell&amp; dataGridViewCell, Int32 columnIndex, Int32 rowIndex, Boolean canCreateNewRow, Boolean validationFailureOccurred)<br><img src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif" align="top"> 在 System.Windows.Forms.DataGridView.SetCurrentCellAddressCore(Int32 columnIndex, Int32 rowIndex, Boolean setAnchorCellAddress, Boolean validateCurrentCell, Boolean throughMouseClick)<br><img src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif" align="top"> 在 System.Windows.Forms.DataGridView.OnCellMouseDown(HitTestInfo hti, Boolean isShiftDown, Boolean isControlDown)<br><img src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif" align="top"> 在 System.Windows.Forms.DataGridView.OnCellMouseDown(DataGridViewCellMouseEventArgs e)<br><img src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif" align="top"> 在 System.Windows.Forms.DataGridView.OnMouseDown(MouseEventArgs e)<br><img src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif" align="top"> 在 System.Windows.Forms.Control.WmMouseDown(Message&amp; m, MouseButtons button, Int32 clicks)<br><img src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif" align="top"> 在 System.Windows.Forms.Control.WndProc(Message&amp; m)<br><img src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif" align="top"> 在 System.Windows.Forms.DataGridView.WndProc(Message&amp; m)<br><img src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif" align="top"> 在 System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message&amp; m)<br><img src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif" align="top"> 在 System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message&amp; m)<br><img src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif" align="top"> 在 System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)<br><img src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif" align="top"> 在 System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG&amp; msg)<br><img src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif" align="top"> 在 System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)<br><img src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif" align="top"> 在 System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)<br><img src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif" align="top"> 在 System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)<br><img src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif" align="top"> 在 System.Windows.Forms.Application.Run(Form mainForm)<br><img src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif" align="top"> 在 KoalaStudio.ksRBAC.PrivilegeConfigTool.Program.Main()<br><img src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif" align="top"> 在 System.AppDomain.nExecuteAssembly(Assembly assembly, String[] args)<br><img src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif" align="top"> 在 System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)<br><img src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif" align="top"> 在 Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()<br><img src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif" align="top"> 在 System.Threading.ThreadHelper.ThreadStart_Context(Object state)<br><img src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif" align="top"> 在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)<br><img src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif" align="top"> 在 System.Threading.ThreadHelper.ThreadStart()<br><img src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif" align="top"></p> <p> 而且比较可恶的是,抛出异常的地方竟然是在Program.cs里,指向 </p> <p><img src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif" align="top">Application.Run(new FrmMain()); </p> <p> 这让人怎么调试,试了N回,也没发现原因。<br> 于是上网找了找,竟然真有解决的方法,其作者也并未解决这个问题,而是采取了折衷的方法,用了一个过渡组件来解决: </p> <p><img src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif" align="top">BindingSource bindingSource = new BindingSource();<br><img src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif" align="top">bindingSource.DataSource = resources;<br><img src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif" align="top">dgvResource.DataSource = bindingSource; </p> <p> 问题可以解决,但又有了新的问题,因为使用了BindingSource,因此对与DataGridView的更改并不能同步到Ilist<resource>对象上,所以还是不能用这种方法,继续研究。<br> 上国外的网站看了看,终于找到了问题的原因,在向DataGridView绑定Ilist<t>类型的对象是,如果对象的成员为0,那么就会出现<br>此问题。而且即使重新绑定DataGridView的数据源,也会继续存在此问题,解决的方法就是在向DataGridView绑定Ilist<t>对象是,要保证其中至少有一个成员,否则宁可不绑定(虽然不是最完美的解决方法)。<br> 其实最好的方法,是用BindingList<t>对象代替Ilist<t>对象作为DataGridView的数据源,即可彻底解决此问题,而且能实现DataGridView修改时与数据源的自动更新。但现在没有时间,只能先这样用了。</t></t></t></t></resource></p></resource></resource>

DataGridView绑定Ilist对象,出现IndexOutOfRangeException错误的解决方法 - 笨笨的熊窝 - 博客园

你可能感兴趣的:(windows,Microsoft)