I"m do the efforts to include data as one by one row to a datagridview right here is mine code and also it says:

"Index was the end of range. Must be non-negative and less than the dimension of the collection parameter name:index"

What walk this mean? What is the trouble in my code?

String Sqlstr2 = "select ItemName indigenous Item whereby ItemID = "" + tbItemID.Text + """;db.DataRead(Sqlstr2);string ItemName = db.dr<"ItemName">.ToString(); DataGridView dataGridView1 = brand-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<> heat = brand-new string<> firstColum, secondColum, thirdColum, fourthColum, fifthColum ;dataGridView1.Rows.Add(row);

You are watching: Index was out of range. must be non-negative and less than the size of the collection

c# datagridview
boost this question
edited Dec 24 "19 in ~ 13:13


33.5k2222 gold badges177177 silver badges238238 bronze badges
asked Oct 3 "13 at 14:46

29711 gold badge33 silver- badges44 bronze title
include a comment |

5 answers 5

energetic oldest Votes

See more: How To Get Paint Off A Mirror, How To Remove Paint Splatters From Your Mirror

The error says "The table of contents is out of range". That method you to be trying to index an object with a worth that was no valid. If you have two books, and also I ask you to provide me your third book, you will certainly look in ~ me funny. This is the computer system looking in ~ you funny. You said - "create a collection". So the did. However initially the collection is empty: not just is over there nothing in the - it has actually no an are to hold anything. "It has no hands".

Then you said "the very first element that the repertoire is currently "ItemID"". And the computer system says "I never ever was asked come create an are for a "first item"." I have no hands to organize this item friend are offering me.

In regards to your code, you created a view, however never mentioned the size. You need a

dataGridView1.ColumnCount = 5;Before do the efforts to access any columns. Modify

DataGridView dataGridView1 = new DataGridView();dataGridView1.Columns<0>.Name = "ItemID";to

DataGridView dataGridView1 = brand-new DataGridView();dataGridView1.ColumnCount = 5;dataGridView1.Columns<0>.Name = "ItemID";See http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.columncount.aspx