Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

.net Gridview Paging issue with Row Command

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

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…
In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

856 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