索引超出范围异常。必须是非负数且小于集合的大小。参数名称:index [英] Index out of range exception. Must be non-negative and less than the size of the collection. Parameter name: index
本文介绍了索引超出范围异常。必须是非负数且小于集合的大小。参数名称:index的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
string greenColor = G跨度>;
OleDbCommand cmd = new OleDbCommand( 从LED_DATA中选择ID,其中color =' + greenColor + ' ,con);
OleDbDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
int r = Convert.ToInt16(reader [ 0 ]。ToString());
for ( int c = 0 ; c < 8 ; c ++)
{
dataGridView1.Rows [r] .Cells [c] .Style.ForeColor = Color.Green;
if ( this .dataGridView1.ColumnCount == 7 )
{
break ;
}
}
}
错误是:索引超出范围。必须是非负数且小于集合的大小。参数名称:索引
请尽快帮助我..我迫切需要它。
解决方案
for ( int c = 0 ; c < 7 ; c ++ )
显然你的网格有7列,所以在你的循环中你必须从0到6。
要么你的table没有8列,或者您正在读取的行数,因为每行中的第一列与DataGridView的行数不匹配。
使用调试器:在行上放置一个断点
dataGridView1.Rows [r] .Cells [c] .Style.ForeColor = Color.Green;
并查看r
和c
的值来检查哪个是范围。
string greenColor = "G";
OleDbCommand cmd = new OleDbCommand("Select ID from LED_DATA where color='" + greenColor + "'", con);
OleDbDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
int r = Convert.ToInt16(reader[0].ToString());
for (int c = 0; c < 8; c++)
{
dataGridView1.Rows[r].Cells[c].Style.ForeColor = Color.Green;
if (this.dataGridView1.ColumnCount == 7)
{
break;
}
}
}
error is: Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index
Please help me as soon as possible.. I need it urgently.
解决方案
for (int c = 0; c < 7; c++)
Apparently your grid has 7 columns, so in your loop you have to go from 0 to to 6.
Either your table does not have 8 columns, or the number of rows you are reading as the first column in each row does not match the rows count for the DataGridView.
Use the debugger: put a breakpoint on the line
dataGridView1.Rows[r].Cells[c].Style.ForeColor = Color.Green;
And look at the value ofr
andc
to check which is out of range.
这篇关于索引超出范围异常。必须是非负数且小于集合的大小。参数名称:index的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文