指数超出范围。必须是非负数且小于集合的大小。参数名称:index- [英] Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index-

查看:104
本文介绍了指数超出范围。必须是非负数且小于集合的大小。参数名称:index-的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码...我试图在这里显示Datagridview选择的单元格=真..我在_RowColIndex中找到了哪些RowIndexes和ColumnIndexed但是我得到以下异常..

at System.Collections.ArrayList.get_Item(Int32 index)

在System.Windows.Forms.DataGridViewRowCollection.SharedRow(Int32 rowIndex)

在System.Windows.Forms。 DataGridViewRowCollection.get_Item(Int32 index)


plesae建议我哪里错了..!



private List< int [] > _RowColIndex = new List< int []>();

i have following code...i m trying here to show Datagridview selected cells=true.. which RowIndexes and ColumnIndexed i have found in _RowColIndex..but i m getting following exception..
at System.Collections.ArrayList.get_Item(Int32 index)
at System.Windows.Forms.DataGridViewRowCollection.SharedRow(Int32 rowIndex)
at System.Windows.Forms.DataGridViewRowCollection.get_Item(Int32 index)

plesae suggest me where i m wrong..!

private List<int[]> _RowColIndex = new List<int[]>();

private List<int[]> _RowColIndex = new List<int[]>();
if (Datagrid1.Rows.Count <= 0) return;
               foreach (var GetRowColIndex in _RowColIndex)
               {
                  Datagrid1.Rows[Convert.ToInt32(GetRowColIndex[0])].Cells[Convert.ToInt32(GetRowColIndex[1])].Selected = true;
               }

推荐答案

仅仅因为列表存在,并不意味着其中的项目具有多于零个元素:

在循环中放置一个断点(或者在发生异常时使用调试器)并仔细查看GetRowColIndex - 它将是一个int数组,但它必须至少包含两个元素或者您的代码将失败。



然后由您来了解duff元素如何进入您的列表! :笑:
Just because the list exists, doesn't mean that the items within it have more than zero elements:
put a breakpoint in the loop (or use the debugger when the exception occurs) and have a close look at GetRowColIndex - it'll be an array of ints, but it has to have at least two elements or you code will fail.

Then it's up to you to find out how a duff element got into your list! :laugh:


Datagrid1.Rows[Convert.ToInt32(GetRowColIndex[0])].Cells[Convert.ToInt32(GetRowColIndex[1])].Selected = true;





在上面的代码中,输入断点并检查 GetRowColIndex [0] 的值和 GetRowColIndex [1] ..


这篇关于指数超出范围。必须是非负数且小于集合的大小。参数名称:index-的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆