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

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

提交回复
热议问题