Tuan Nguyen

Tuan Nguyen

  • NA
  • 20
  • 1.3k

Error while paging in Gridview: "Index was out of range."

Oct 30 2019 11:44 PM
Hi everyone, I have got an error "Index was out of range. Must be non-negative and less than the size of the collection." while paging in Gridview.
 
I tried to get value from a hidden Label while paging and this error happened. Please help me, thank you.
  1. <asp:GridView ID="GridView2" runat="server" ForeColor="#333333" AllowPaging="True"  
  2. AutoGenerateColumns="False" DataKeyNames="id" PageSize="1" CssClass="table table-striped table-bordered table-hover"  
  3. OnPageIndexChanging="GridView2_PageIndexChanging" OnRowCancelingEdit="GridView2_RowCancelingEdit"  
  4. OnRowDeleting="GridView2_RowDeleting" OnRowEditing="GridView2_RowEditing"  
  5. OnRowUpdating="GridView2_RowUpdating" OnRowDataBound="GridView2_RowDataBound">  
  6. <Columns>  
  7. <asp:BoundField DataField="id" ItemStyle-CssClass="" HeaderStyle-CssClass="" FooterStyle-CssClass="" />  
  8. <asp:TemplateField ItemStyle-CssClass="" HeaderStyle-CssClass="" FooterStyle-CssClass="">  
  9. <ItemTemplate>  
  10. <asp:Label ID="lblFruitCategoryID" runat="server" Text='<%# Eval("fruitCategoryID") %>'></asp:Label>  
  11. </ItemTemplate>  
  12. </asp:TemplateField>  
  13. <asp:TemplateField ItemStyle-CssClass="" HeaderStyle-CssClass="" FooterStyle-CssClass="">  
  14. <ItemTemplate>  
  15. <asp:Label ID="lblFruitSubCategoryID" runat="server" Text='<%# Eval("id") %>'></asp:Label>  
  16. </ItemTemplate>  
  17. </asp:TemplateField>  
  18. <asp:TemplateField ItemStyle-HorizontalAlign="Center">  
  19. <ItemTemplate>  
  20. <asp:Image ID="imgFruitImage" runat="server" Width="100px" Height="100px" />  
  21. </ItemTemplate>  
  22. <EditItemTemplate>  
  23. <asp:Image ID="imgEditFruitImage" runat="server" Width="100px" Height="100px" />  
  24. <%--<asp:Label ID="imgEditFruitImage" runat="server" Text=""></asp:Label>--%>  
  25. <asp:FileUpload ID="fileEdit" runat="server" CssClass="btn btn-blue-2" />  
  26. </EditItemTemplate>  
  27. </asp:TemplateField>  
  28. <asp:TemplateField HeaderText="Sub fruit category">  
  29. <ItemTemplate>  
  30. <asp:LinkButton ID="lbSubFruitCategoryName" runat="server" CommandName="view" CommandArgument='<%# Bind("id") %>' Text='<%# Bind("subFruitCategoryName") %>'></asp:LinkButton>  
  31. </ItemTemplate>  
  32. <EditItemTemplate>  
  33. <asp:TextBox ID="txtEditSubFruitCategoryName" CssClass="form-control" runat="server" Text='<%# Bind("subFruitCategoryName") %>'></asp:TextBox>  
  34. </EditItemTemplate>  
  35. </asp:TemplateField>  
  36. <asp:TemplateField HeaderText="Fruit category">  
  37. <ItemTemplate>  
  38. <asp:Label ID="Label1" runat="server" Text='<%# Bind("categoryName") %>'></asp:Label>  
  39. </ItemTemplate>  
  40. <EditItemTemplate>  
  41. <asp:DropDownList ID="ddlEditCategoryName" CssClass="form-control" runat="server"></asp:DropDownList>  
  42. </EditItemTemplate>  
  43. </asp:TemplateField>  
  44. <asp:TemplateField HeaderStyle-Width="60px">  
  45. <ItemTemplate>  
  46. <asp:Button ID="btn_Edit2" runat="server" Text="Edit" CommandName="Edit" CssClass="btn btn-blue-2" />  
  47. </ItemTemplate>  
  48. <EditItemTemplate>  
  49. <asp:Button ID="btn_Update2" runat="server" Text="Update" CommandName="Update" CssClass="btn btn-blue-2" />  
  50. <asp:Button ID="btn_Cancel2" runat="server" Text="Cancel" CommandName="Cancel" CssClass="btn btn-default" />  
  51. </EditItemTemplate>  
  52. </asp:TemplateField>  
  53. <asp:TemplateField ItemStyle-Width="100px">  
  54. <ItemTemplate>  
  55. <asp:LinkButton ID="LkB11" CssClass="btn btn-danger" runat="server" CommandName="Delete"><i class="fa fa-trash-o"></i> Delete</asp:LinkButton>  
  56. </ItemTemplate>  
  57. </asp:TemplateField>  
  58. </Columns>  
  59. <HeaderStyle BackColor="#009b97" Font-Bold="True" ForeColor="White" Height="35" />  
  60. </asp:GridView>  
And code behind for Paging is:
  1. protected void GridView2_PageIndexChanging(object sender, GridViewPageEventArgs e)  
  2. {  
  3. GridView2.PageIndex = e.NewPageIndex;  
  4. Admin_BO_Fruits obj_Admin_BO_Fruits = new Admin_BO_Fruits();  
  5. Label lbl = (Label)GridView2.Rows[e.NewPageIndex].FindControl("lblFruitCategoryID");  
  6. obj_Admin_BO_Fruits.Admin_BO_FruitCategoryID = Convert.ToInt32(lbl.Text);  
  7. GetAllSubFruitCategoryByFruitCategoryID(obj_Admin_BO_Fruits);  
  8. //GridView2.DataBind();  
  9. }  

Answers (1)