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

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

问题描述

我想通过一个行添加数据为一体,在这里一个DataGridView是我的代码和它说:
索引超出范围必须大于集合的大小非负少
参数名称索引:这是什么意思吗?有没有在我的代码中的任何问题。



 字符串Sqlstr2 =从项目选择,其中ITEMNAME条目ID ='+ tbItemID.Text + '; 
db.DataRead(Sqlstr2);
ItemName字符串= db.dr [ITEMNAME]的ToString()。

的DataGridView dataGridView1 =新的DataGridView();

dataGridView1.Columns [0] .Name点=项目编号;
dataGridView1.Columns [1] .Name点=ITEMNAME;
dataGridView1.Columns [2] .Name点=数量;
dataGridView1.Columns [3] .Name点=单价;
dataGridView1.Columns [4] .Name点=金额;

串firstColum = tbItemID.Text;
串secondColum = ITEMNAME;
串thirdColum = tbQuantity.Text;
串fourthColum = Convert.ToString(单价);
串fifthColum = Convert.ToString(总和);

的String []行=新的String [] {firstColum,secondColum,thirdColum,fourthColum,fifthColum};
dataGridView1.Rows.Add(行);


解决方案

你不添加列到你的 DataGridView的

 的DataGridView dataGridView1 =新的DataGridView(); //创建新的网格

dataGridView1.Columns [0] .Name点=项ID; //指其不存在

是否清楚现在为什么你得到一个异常?



您使用的列来修复错误之前添加此行

  dataGridView1.ColumnCount = 5; 


I'm trying to add data as one by one row to a datagridview here is my code and it says: "Index was out of range. Must be non-negative and less than the size of the collection parameter name:index" what does this means ? is there any problem in my code

String Sqlstr2 = "select ItemName from Item where ItemID = '" + tbItemID.Text + "'";
db.DataRead(Sqlstr2);
string ItemName = db.dr["ItemName"].ToString(); 

DataGridView dataGridView1 = new DataGridView();

dataGridView1.Columns[0].Name = "ItemID";
dataGridView1.Columns[1].Name = "ItemName";
dataGridView1.Columns[2].Name = "Qty";
dataGridView1.Columns[3].Name = "UnitPrice";
dataGridView1.Columns[4].Name = "Amount";

string firstColum = tbItemID.Text;
string secondColum = ItemName;
string thirdColum = tbQuantity.Text;
string fourthColum = Convert.ToString(UnitPrice);
string fifthColum = Convert.ToString(sum);

string[] row = new string[]{ firstColum, secondColum, thirdColum, fourthColum, fifthColum };
dataGridView1.Rows.Add(row);

解决方案

You're not adding columns to your DataGridView

DataGridView dataGridView1 = new DataGridView();//Create new grid

dataGridView1.Columns[0].Name = "ItemID";// refer to column which is not there 

Is it clear now why you get an exception?

Add this line before you use columns to fix the error

dataGridView1.ColumnCount = 5;

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

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