40

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);
Cœur
  • 37,241
  • 25
  • 195
  • 267
user2566013
  • 407
  • 1
  • 4
  • 4
  • 1
    Just a simple `dataGridView.Columns.Length` (or something like that) will tell you how many columns it actually has. – Arran Oct 03 '13 at 14:50

5 Answers5

48

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.

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

Floris
  • 45,857
  • 6
  • 70
  • 122
  • You're welcome. I feel it's the same answer as the one you accepted, although I came up with it independently (and, I might add, first. :-) ) – Floris Oct 03 '13 at 15:16
  • Hi Floris. I have got this error "column count property cannot be set on a databound datagridview control c#", at "dataGridView1.ColumnCount = 2;" Please help me to resolve this. – Sanjeev4evr Apr 05 '14 at 07:11
  • @Sanjeev4evr - your error message suggests to me that if the property cannot be set, it must have been defined by something else. Perhaps a "databound datagridview" has the column count fixed by the data. Does it work if you leave that line out? – Floris Apr 05 '14 at 11:41
  • When I removed that line it thrown the same exception "Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index" – Sanjeev4evr Apr 05 '14 at 11:55
  • @sanjeev4ever I suggest you post a separate question with more detail about your specific situation, then put a link here. – Floris Apr 05 '14 at 12:49
  • Here is the Question. http://stackoverflow.com/questions/22904248/column-count-property-cannot-be-set-on-a-databound-datagridview-control-c-sharp. – Sanjeev4evr Apr 07 '14 at 05:30
  • @Sanjeev4evr - I see that you found the answer to your other question before I even had a chance to look at it! – Floris Apr 07 '14 at 19:25
  • 1
    Yes. Thanks for looking back to my question. You can help me some other time. – Sanjeev4evr Apr 08 '14 at 04:08
  • Mine was a different case, but this answer gave me the idea that I am trying to add points before adding series to the chart. Thank you. – Neo Sep 20 '21 at 15:28
26

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;
Sriram Sakthivel
  • 72,067
  • 7
  • 111
  • 189
8

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

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.

enter image description here

Sachin
  • 40,216
  • 7
  • 90
  • 102
2

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

meilke
  • 3,280
  • 1
  • 15
  • 31
1

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.

Kcvin
  • 5,073
  • 2
  • 32
  • 54