DataGridView绑定Ilist对象,出现IndexOutOfRangeException错误的解决方法
2008-04-24 12:21
661 查看
DataGridView绑定Ilist对象,出现IndexOutOfRangeException错误的解决方法 我是这样绑定DataGridVeiw的:
IList resources = new List();
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对象上,所以还是不能用这种方法,继续研究。
上国外的网站看了看,终于找到了问题的原因,在向DataGridView绑定Ilist类型的对象是,如果对象的成员为0,那么就会出现
此问题。而且即使重新绑定DataGridView的数据源,也会继续存在此问题,解决的方法就是在向DataGridView绑定Ilist对象是,要保证其中至少有一个成员,否则宁可不绑定(虽然不是最完美的解决方法)。
其实最好的方法,是用BindingList对象代替Ilist对象作为DataGridView的数据源,即可彻底解决此问题,而且能实现DataGridView修改时与数据源的自动更新。但现在没有时间,只能先这样用了。
DataGridView绑定Ilist对象,出现IndexOutOfRangeException错误的解决方法 - 笨笨的熊窝 - 博客园
相关文章推荐
- DataGridView绑定Ilist对象,出现IndexOutOfRangeException错误的解决方法
- 解决java.lang.IllegalArgumentException: pointerIndex out of range 或者 arrayindexoutofboundsexception的错误
- python创建cocos2d-x出现list index out of range的问题解决方法
- 解决java.lang.IllegalArgumentException: pointerIndex out of range 或者 arrayindexoutofboundsexception的错误
- eclipse Java文件打开异常解决方法 :java.lang.StringIndexOutOfBoundsException: String index out of range: 26
- 解决java.lang.IllegalArgumentException: pointerIndex out of range的错误
- 嵌套两层滑动view下,IllegalArgumentException: pointerIndex out of range的解决方法
- DataGrid的ScrollIntoView方法导致 an argument out of range exception(指定的参数已超出有效值的范围)错误的解决
- 解决多指操作放大缩小 指针错误 java.lang.IllegalArgumentException: pointerIndex out of range
- Android Studio 2.0+发布APK出现ExternalSystemException: String index out of range: -123错误
- C# B/S程序中使用DropDownList出现System.ArgumentOutOfRangeException: “DropDownList1”有一个无效 SelectedValue,因为它不在项目列表中的解决方法
- AsParallel \AsQueryable<T>().ToList() [System.ArgumentOutOfRangeException was unhandled" 索引超出范围。必须为非负值并小于集合大小]解决方法
- java.lang.IllegalArgumentException: pointerIndex out of range 问题的两种解决办法
- 关于mysql 出现 1264 Out of range value for column 错误的解决办法
- mysql出现out of memory错误的解决方法
- SQL Server出现System.OutOfMemoryException异常的解决方法
- JSP中内建exception对象时出现500错误的解决方法
- Xcode7中使用gitHub管理代码时出现The local repository is out of date错误的解决方法
- Android4.1上出现java.lang.IllegalArgumentException: pointerIndex out of range
- MySQL错误 Out of range value adjusted for column的解决方法