Solved

.net Gridview Paging issue with Row Command

Posted on 2010-11-18
2
1,015 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
Comment Utility
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
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
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.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

771 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now