指数超出范围。必须是非负且小于集合的大小 [英] Index was out of range. Must be nonnegative and less than the size of the collection

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

问题描述

<blockquote class="quote"><div class="op">Quote:</div><blockquote class="quote"><div class="op">Quote:</div> try
            {
                if (cn.State.ToString() != "Open")
                    cn.Open();


                for (int i = 0; i < 10; i++)
                {
                    cmd = new SqlCommand("select fahsName from Name_fahs where Number_N='1'  and Number_of_each_Name='"+i+1+"'", cn);
                    dr = cmd.ExecuteReader();
                    dr.Read();
                    dataGridView1.Rows[i].Cells[i].Value = dr["fahsName"].ToString();
                    
                    dr.Close();
                }
               
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + "jwnjew ");
            }
            finally 
            {<pre lang="c#">



cmd.Cancel( );

cn.Close();

}



我尝试过:




cmd.Cancel();
cn.Close();
}

What I have tried:

<blockquote class="quote"><div class="op">Quote:</div><blockquote class="quote"><div class="op">Quote:</div> try
            {
                if (cn.State.ToString() != "Open")
                    cn.Open();


                for (int i = 0; i < 10; i++)
                {
                    cmd = new SqlCommand("select fahsName from Name_fahs where Number_N='1'  and Number_of_each_Name='"+i+1+"'", cn);
                    dr = cmd.ExecuteReader();
                    dr.Read();
                    dataGridView1.Rows[i].Cells[i].Value = dr["fahsName"].ToString();
                    
                    dr.Close();
                }
               
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + "jwnjew ");
            }
            finally 
            {<pre lang="c#">



cmd.Cancel( );

cn.Close();

}


cmd.Cancel();
cn.Close();
}

推荐答案

查看错误消息:

Look at the error message:
Index was out of range. Must be nonnegative and less than the size of the collection

如果它尝试的话它就不会更清楚了!



你正在访问数组通过索引,它对于数组来说是负数或太大。例如,

It couldn;t be a lot clearer if it tried!

You are accessing an array via an index and it is either negative or too big for the array. For example,

int[] m = new int[3];

只能以<作为

Can only be access ed as

int i1 = m[0];
int i2 = m[1];
int i3 = m[2];

任何其他值会给你这个错误。



在你的代码中,你试图在位置访问DGV

Any other value will give you this error.

In your code, you are trying to access the DGV at locations

[0,0]
[1,1]
[2,2]
[3,3]
[4,4]
[5,5]
[6,6]
[7,7]
[8,8]
[9,9]

所以除非你的DGV在至少10行和至少10列,它将失败。

我怀疑你的意思是引用所有行的特定列,而不是对角线。



为什么不做单个SqlCommand并使用

So unless your DGV has at least 10 rows and at least 10 columns, it will fail.
I suspect that you meant to reference a specific column for all the rows, rather than a diagonal.

Why not do a single SqlCommand and with

... WHERE Number_Name = '1' AND Number_of_each_Name BETWEEN 0 AND 9

而不是多次往返服务器?

instead of doing multiple round trips to the server?


dataGridView1.Rows[i].Cells[i].Value = dr["fahsName"].ToString();





检查i在此处是否有效。如果我是3并且没有至少4行,那么你将得到这个错误,或者如果第四行没有至少4行,你将得到错误。基本上,只有在您已经使用足够的行和列填充数据网格视图时,此代码才有效。如果您正在寻找构建网格,那么当您引用它们时,事情就不会存在,您需要明确地创建它们。所以google如何将行和单元格添加到datagridview。



Check that "i" is valid here. If i is 3 and there are not at least 4 rows then you'll get this error, or if the fourth row doesn't have at least 4 cells you'll get the error. Basically this code will only work if you've already populated your datagridview with enough rows and columns. If you're looking to build the grid as you go then things don't come into existence as you reference them, you'll need to explicitly create them. So google how to add rows and cells to a datagridview.


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

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