Solved

Paging in GridView

Posted on 2011-02-23
24
404 Views
Last Modified: 2012-05-11
When click on the second page of a gridview I get the following error;

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

error line;

Line 213:            string Assignid = ((Label)(GridView1.Rows[rowIndex].FindControl("lblAssignId"))).Text.ToString();

the error is generated from this line of markup

          <asp:TemplateField ShowHeader="False" meta:resourcekey="TemplateFieldResource5">
              <ItemTemplate>
                     <asp:LinkButton ID="lbA" runat="server" CausesValidation="False" CommandName="Ap"  CommandArgument='<%# Container.DisplayIndex %>' Text="Approve" meta:resourcekey="lbApproveResource1"></asp:LinkButton>
                </ItemTemplate>
            </asp:TemplateField>


code-behind

  protected void ItemCommand(object sender, GridViewCommandEventArgs e)

    {
        int rowIndex = Convert.ToInt32(e.CommandArgument);

            string Assignid = ((Label)(GridView1.Rows[rowIndex].FindControl("lblAssignId"))).Text.ToString();
pls help me tofix the error.

thanks

ayha
0
Comment
Question by:ayha1999
  • 12
  • 12
24 Comments
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 34963160
int rowIndex = Convert.ToInt32(e.CommandArgument);

And what is the value of rowIndex ?
0
 
LVL 7

Author Comment

by:ayha1999
ID: 34963243
rowIndex value is value of "Container.DisplayIndex":  from the following field

  <asp:LinkButton ID="lbA" runat="server" CausesValidation="False" CommandName="Ap"  CommandArgument='<%# Container.DisplayIndex %>' Text="Approve" meta:resourcekey="lbApproveResource1"></asp:LinkButton>
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 34963253
But what is the value 2, -37, 987657....
0
 
LVL 7

Author Comment

by:ayha1999
ID: 34963304
did I mentioned 2, -37, 987657.... anywhere in my question? only two rows there in my gridview. when click on the 2nd page the error occurs.
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 34963379
From your question
Index was out of range. Must be non-negative and less than the size of the collection.


I am asking you, now for the third time, what is the value of the index.  Believe me it should help in solving your problem.
0
 
LVL 7

Author Comment

by:ayha1999
ID: 34963437
value of rowIndex is 2.
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 34963513
OK, how many rows do you have in your gridview?
0
 
LVL 7

Author Comment

by:ayha1999
ID: 34963522
only two rows.
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 34963547
That is your problem.
The first row has an index of 0, the second has an index of 1.  
0
 
LVL 7

Author Comment

by:ayha1999
ID: 34963602
yes. how can I fix it?
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 34963623
The obvious fix is
int rowIndex = Convert.ToInt32(e.CommandArgument) - 1;

but I don't know if you actually send a zero for the first row.  It all depends on what you use for the e.CommandArgument
0
 
LVL 7

Author Comment

by:ayha1999
ID: 34963667
the purpose of the rowIndex is:

protected void ItemCommand(object sender, GridViewCommandEventArgs e)
{
  int rowIndex = Convert.ToInt32(e.CommandArgument);

            string RequestId = ((Label)(GridView1.Rows[rowIndex].FindControl("lblRequestId"))).Text;
            if (User.IsInRole("Admin"))
            {
                Response.Redirect("~/PassEntry.aspx?RequestId=" + RequestId.ToString());
            }
            else
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 44

Expert Comment

by:AndyAinscow
ID: 34963719
I don't see your complete code, I don't know what it should be doing.

I have helped you find the error and told you why you get the error.  Exactly what you do is really up to your requirements.

IF the index is always 1 too many then just subtract 1.
If the index is usually correct then you need to check if the index is greater than the number of rows (or less than zero) and take appropriate action (leave the function / give a warning / ....)
0
 
LVL 7

Author Comment

by:ayha1999
ID: 34963752
even , int rowIndex = Convert.ToInt32(e.CommandArgument) - 1; give me the same error.
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 34963772
What is the value of rowIndex when that gives the error ?
0
 
LVL 7

Author Comment

by:ayha1999
ID: 34963832
rowIndex=1
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 34963951
OK.
This is what you say
GridView1.Rows[1] gives you an 'Index out of range' error, and GridView1 has 2 rows at that point.

That makes me think that GridView1 isn't what you think it is.

Have some code like this AND CHECK THE VALUES

int rowIndex = Convert.ToInt32(e.CommandArgument) - 1;
int rowCount = GridView1.Rows.Count;
0
 
LVL 7

Author Comment

by:ayha1999
ID: 34972708
rowIndex = 1
rowCount = 1
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 34973436
rowCount = 1  means (as I told you earlier) that the only valid index is 0.  Hence the error.
0
 
LVL 7

Author Comment

by:ayha1999
ID: 34973485
so what is the solution here? I think by changing the following lines it can be solved.

  int rowIndex = Convert.ToInt32(e.CommandArgument);

            string RequestId = ((Label)(GridView1.Rows[rowIndex].FindControl("lblRequestId"))).Text;

I want to retrive the value of lblRequestId Label in the item command. that's what I am doing here. is there any other way I can retrive the value?

but I have no idea how to do it.
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 34973555
I don't know where this e.CommandArgument comes from.
I also don't know if this function is being called multiple times, sometimes with the value you expect and then with an out of range value.

You should know these things AND, more importantly, what you need to do in those cases.


I **guess** you have always a value 1 more than the actual row index.
0
 
LVL 7

Author Comment

by:ayha1999
ID: 34973763
e.CommandArugment comes from;

<asp:LinkButton ID="lbA" runat="server" CausesValidation="False" CommandName="Ap"  CommandArgument='<%# Container.DisplayIndex %>' Text="Approve" meta:resourcekey="lbApproveResource1"></asp:LinkButton>

I have linkbuttons in the gridvew1. when user click on any link button, I want to retrieve the value of the clicked row. (value of any cell(0) for example.) to find the clicked row I use the above e.CommandArugment.

is there any other way to get the value? I think that will solve the paging problem.

thanks
0
 
LVL 44

Accepted Solution

by:
AndyAinscow earned 250 total points
ID: 34973819
If this value is always 1 too high then just subtract 1:
int rowIndex = Convert.ToInt32(e.CommandArgument) - 1;

You should be able to check the values yourself, I don't know where the value being passed into the function is generated or where it comes from should it be hard coded somewhere.
0
 
LVL 7

Author Comment

by:ayha1999
ID: 34974116
without deducting it the problem is solved.

 if (e.CommandName == "CreateWo")
        {
            int rowIndex = Convert.ToInt32(e.CommandArgument);
            string RequestId = ((Label)(GridView1.Rows[rowIndex].FindControl("lblRequestId"))).Text;
        }
        else if (e.CommandName == "Approve")  <----- only 'else' was here. I changed to 'else if'
        {
            int rowIndex = Convert.ToInt32(e.CommandArgument);
           // int rowCount = GridView1.Rows.Count;

thank u very much for your time and support
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Need help on C# Linq query on object 6 53
Error in script 11 48
Error on link 14 40
IIS issues - Convert to Application - APS.NET v4.0 3 24
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.
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…

863 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

22 Experts available now in Live!

Get 1:1 Help Now