turpya
asked on
Datagrid error when paging: Index was out of range. Must be non-negative and less than the size of the collection.
I have a gridview which lists people's names retrieved from a SQL database. I am assigning the row index to the image buttons' command argument when each row is created. The image buttons work fine when on the first page of data. If I go to page 2 of the gridview and click on an image button, I receive the following error:
Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.ArgumentOutOfRangeE xception: Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index
Source Error:
Line 366: Case "Notes"
Line 367: Dim index As Integer = Convert.ToInt32(e.CommandA rgument)
Line 368: Dim selectedRow As GridViewRow = GridView1.Rows(index)
Line 369:
Line 370: lblID = selectedRow.Cells(0).FindC ontrol("lb lID")
Source File: e:\inetpub\people\manage_i ndividuals .aspx.vb Line: 368
Stack Trace:
[ArgumentOutOfRangeExcepti on: Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index]
System.Collections.ArrayLi st.get_Ite m(Int32 index) +2776637
System.Web.UI.WebControls. GridViewRo wCollectio n.get_Item (Int32 index) +10
_default.GridView1_RowComm and(Object sender, GridViewCommandEventArgs e) in e:\inetpub\vs137121\pastor al-care\pe ople\manag e_individu als.aspx.v b:368
System.Web.UI.WebControls. GridView.O nRowComman d(GridView CommandEve ntArgs e) +105
System.Web.UI.WebControls. GridView.H andleEvent (EventArgs e, Boolean causesValidation, String validationGroup) +76
System.Web.UI.WebControls. GridView.O nBubbleEve nt(Object source, EventArgs e) +95
System.Web.UI.Control.Rais eBubbleEve nt(Object source, EventArgs args) +35
System.Web.UI.WebControls. GridViewRo w.OnBubble Event(Obje ct source, EventArgs e) +117
System.Web.UI.Control.Rais eBubbleEve nt(Object source, EventArgs args) +35
System.Web.UI.WebControls. ImageButto n.OnComman d(CommandE ventArgs e) +115
System.Web.UI.WebControls. ImageButto n.RaisePos tBackEvent (String eventArgument) +171
System.Web.UI.WebControls. ImageButto n.System.W eb.UI.IPos tBackEvent Handler.Ra isePostBac kEvent(Str ing eventArgument) +7
System.Web.UI.Page.RaisePo stBackEven t(IPostBac kEventHand ler sourceControl, String eventArgument) +11
System.Web.UI.Page.RaisePo stBackEven t(NameValu eCollectio n postData) +33
System.Web.UI.Page.Process RequestMai n(Boolean includeStagesBeforeAsyncPo int, Boolean includeStagesAfterAsyncPoi nt) +5102
-------------------------- ---------- ---------- ---------- ---------- ---------- ----
Version Information: Microsoft .NET Framework Version:2.0.50727.42; ASP.NET Version:2.0.50727.42
Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.ArgumentOutOfRangeE
Parameter name: index
Source Error:
Line 366: Case "Notes"
Line 367: Dim index As Integer = Convert.ToInt32(e.CommandA
Line 368: Dim selectedRow As GridViewRow = GridView1.Rows(index)
Line 369:
Line 370: lblID = selectedRow.Cells(0).FindC
Source File: e:\inetpub\people\manage_i
Stack Trace:
[ArgumentOutOfRangeExcepti
Parameter name: index]
System.Collections.ArrayLi
System.Web.UI.WebControls.
_default.GridView1_RowComm
System.Web.UI.WebControls.
System.Web.UI.WebControls.
System.Web.UI.WebControls.
System.Web.UI.Control.Rais
System.Web.UI.WebControls.
System.Web.UI.Control.Rais
System.Web.UI.WebControls.
System.Web.UI.WebControls.
System.Web.UI.WebControls.
System.Web.UI.Page.RaisePo
System.Web.UI.Page.RaisePo
System.Web.UI.Page.Process
--------------------------
Version Information: Microsoft .NET Framework Version:2.0.50727.42; ASP.NET Version:2.0.50727.42
<asp:TemplateField HeaderText="">
<ItemTemplate>
<asp:Button Text="Edit" CommandName="Edit" CausesValidation="false" runat="server" ID="btEdit" CssClass="btnClass" />
</ItemTemplate>
<EditItemTemplate>
<asp:Button Text="Update" CommandName="Update" CausesValidation="true" runat="server" ID="btUpdate" CssClass="btnClass" />
<asp:Button Text="Cancel" CommandName="Cancel" CausesValidation="false" runat="server" ID="btCancel" CssClass="btnClass" />
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Actions" HeaderStyle-Font-Bold="True" HeaderStyle-HorizontalAlign="Center">
<ItemTemplate>
<asp:ImageButton ImageUrl="/images/view_person.gif" CommandArgument='<%# Container.DataItemIndex %>' Text="Click to view details on this person" CommandName="View" runat="server" ID="ibnView" />
<asp:ImageButton imageURl="/images/remove_person.gif" CommandArgument='<%# Container.DataItemIndex %>' Text="Click to remove this person from the register" CommandName="DeletePerson" runat="server" ID="ibnDelete" />
<asp:ImageButton ImageUrl="/images/notes.gif" CommandArgument='<%# Container.DataItemIndex %>' Text="Click to view notes on this person" CommandName="Notes" runat="server" ID="ibnNotes" />
</ItemTemplate>
</asp:TemplateField>
Protected Sub GridView1_RowCreated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowCreated
If e.Row.RowType = DataControlRowType.DataRow Then
Dim ibn1 As ImageButton
Dim ibn2 As ImageButton
Dim ibn3 As ImageButton
ibn1 = CType(e.Row.FindControl("ibnView"), ImageButton)
ibn2 = CType(e.Row.FindControl("ibnDelete"), ImageButton)
ibn3 = CType(e.Row.FindControl("ibnNotes"), ImageButton)
ibn1.CommandArgument = e.Row.RowIndex
ibn2.CommandArgument = e.Row.RowIndex
ibn3.CommandArgument = e.Row.RowIndex
ibn2.Attributes.Add("onclick", "return " & _
"confirm('Are you sure you want to remove this person?');")
End If
End Sub
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
hmmmm.... try e.row.dataitemindex instead of row index. Paging can be a bear!
ASKER
Thanks Tony.
I removed CommandArgument='<%# Container.DataItemIndex %>' from the template image buttons.
I realised I was assigning the CommandArgument twice: once in RowCreated event...
ibn3.CommandArgument = e.Row.RowIndex, the other on the image button in the gridview template.
Also removed gridview databinding from the PageIndexChanging event - not sure if this had any bearing on anything though.
I removed CommandArgument='<%# Container.DataItemIndex %>' from the template image buttons.
I realised I was assigning the CommandArgument twice: once in RowCreated event...
ibn3.CommandArgument = e.Row.RowIndex, the other on the image button in the gridview template.
Also removed gridview databinding from the PageIndexChanging event - not sure if this had any bearing on anything though.
ASKER
I am doing what the article says... "To get the index, get it from current Row's RowIndex property, for example in RowCreated event".
I am using paging (AllowPaging="True"). It works on page one of the gridview, but not on any other pages - if not on page 1 and I click a button, I get the aforementioned error.