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

后端 未结 5 1762
挽巷
挽巷 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: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;
    

提交回复
热议问题