Solved

.net Gridview Paging issue with Row Command

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
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.

685 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