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

后端 未结 5 1744
挽巷
挽巷 2020-12-10 01:03

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

相关标签:
5条回答
  • 2020-12-10 01:41

    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

    0 讨论(0)
  • 2020-12-10 01:44

    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;
    
    0 讨论(0)
  • 2020-12-10 01:47

    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.

    0 讨论(0)
  • 2020-12-10 01:57

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

    0 讨论(0)
  • 2020-12-10 02:06

    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

    0 讨论(0)
提交回复
热议问题