Solved

.net Gridview Paging issue with Row Command

Posted on 2010-11-18
2
1,035 Views
Last Modified: 2012-05-10
I have a gridview that allows paging and uses a row command event.

The gridview column that caused the issue is:
                        <asp:TemplateField>
                            <ItemTemplate>
                                <asp:LinkButton ID="LinkButton1" Runat="server" OnClientClick="return confirm('Are you sure you want to delete this item?');"
                                CommandName="ConvertDelete" CommandArgument="<%# Container.DataItemIndex %>">Delete</asp:LinkButton>
                            </ItemTemplate>
                            <ItemStyle BackColor="#FFC0C0" Font-Size="X-Small" />
                        </asp:TemplateField>


The codebehind code that causes the issue is:

                Dim index As Integer = Convert.ToInt32(e.CommandArgument)
                Dim row As GridViewRow = gridview1.Rows(index)

The error reads:

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

The index value is greater than the page size.  Everything works on Page 1 but fails on all other pages.

Please help!
0
Comment
Question by:dtechfish
2 Comments
 
LVL 13

Accepted Solution

by:
devlab2012 earned 500 total points
ID: 34163983
Why you are setting CommandArgument to Container.DataItemIndex. Set it directly to the primary key of your table like the ItemID which you can use to delete the record.
0
 
LVL 32

Expert Comment

by:Erick37
ID: 34164037
You  can set the CommandArgument of the LinkButton in the grid's RowCreated event.  

See the example here:

http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.rowcommand(VS.80).aspx
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

785 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question