C# 指数超出范围。必须为非负且小于集合参数 name:index 的大小

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19162248/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 14:15:50  来源:igfitidea点击:

Index was out of range. Must be non-negative and less than the size of the collection parameter name:index

c#datagridview

提问by user2566013

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 mean? What is the 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);

采纳答案by Sriram Sakthivel

You're not adding columns to your DataGridView

你没有在你的 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;

回答by Sachin

what this means ? is there any problem in my code

这是什么意思?我的代码有问题吗

It means that you are accessing a location or index which is not present in collection.

这意味着您正在访问集合中不存在的位置或索引。

To find this, Make sure your Gridview has 5 columns as you are using it's 5th column by this line

要找到这一点,请确保您的 Gridview 有 5 列,因为您正在使用此行的第 5 列

dataGridView1.Columns[4].Name = "Amount";

Here is the image which shows the elements of an array. So if your gridview has less column then the (index + 1)by which you are accessing it, then this exception arises.

这是显示数组元素的图像。因此,如果您的 gridview 的列少于(index + 1)您访问它的列,则会出现此异常。

enter image description here

在此处输入图片说明

回答by meilke

dataGridView1.Columnsis probably of a length less than 5. Accessing dataGridView1.Columns[4]then will be outside the list.

dataGridView1.Columns长度可能小于 5。访问dataGridView1.Columns[4]then 将在列表之外。

回答by Floris

The error says "The index is out of range". That means you were trying to index an object with a value that was not valid. If you have two books, and I ask you to give me your third book, you will look at me funny. This is the computer looking at you funny. You said - "create a collection". So it did. But initially the collection is empty: not only is there nothing in it - it has no space to hold anything. "It has no hands".

错误提示“索引超出范围”。这意味着您试图用无效的值索引一个对象。如果你有两本书,而我请你把你的第三本书给我,你会看我很有趣。这是电脑看着你很有趣。你说 - “创建一个集合”。所以它做到了。但最初这个集合是空的:不仅里面什么都没有——它没有空间容纳任何东西。“它没有手”。

Then you said "the first element of the collection is now 'ItemID'". And the computer says "I never was asked to create space for a 'first item'." I have no hands to hold this item you are giving me.

然后你说“集合的第一个元素现在是'ItemID'”。电脑说“我从来没有被要求为‘第一件物品’创造空间。” 我没有手拿你给我的这个东西。

In terms of your code, you created a view, but never specified the size. You need a

就您的代码而言,您创建了一个视图,但从未指定大小。你需要一个

dataGridView1.ColumnCount = 5;

Before trying to access any columns. Modify

在尝试访问任何列之前。调整

DataGridView dataGridView1 = new DataGridView();

dataGridView1.Columns[0].Name = "ItemID";

to

DataGridView dataGridView1 = new DataGridView();
dataGridView1.ColumnCount = 5;
dataGridView1.Columns[0].Name = "ItemID";

See http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.columncount.aspx

请参阅http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.columncount.aspx

回答by Kamran Binyamin

This error is caused when you have enabled paging in Grid view. If you want to delete a record from grid then you have to do something like this.

当您在网格视图中启用分页时会导致此错误。如果你想从网格中删除一条记录,那么你必须做这样的事情。

int index = Convert.ToInt32(e.CommandArgument);
int i = index % 20;
// Here 20 is my GridView's Page Size.
GridViewRow row = gvMainGrid.Rows[i];
int id = Convert.ToInt32(gvMainGrid.DataKeys[i].Value);
new GetData().DeleteRecord(id);
GridView1.DataSource = RefreshGrid();
GridView1.DataBind();

Hope this answers the question.

希望这能回答这个问题。