Click here to Skip to main content
15,882,114 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi,

I am getting error with this code:
C#
for (int i = 0; i <= gridview.Rows.Count; i++)
{
    if (!xxx.equals(gridview.Rows[i].Cells[0].Text))
    {
        dr = dt.NewRow();
        dr[0] = */adding value*/
        dt.Rows.Add(dr);
    }
}


I am getting an error with this gridview.Rows[i].Cells[0].Text line.
Error which am getting is:
Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index

What may be the reason for this error?

Please help me
Posted
Updated 5-Mar-13 2:22am
v5
Comments
ZurdoDev 5-Mar-13 6:56am    
Either Rows[i] is null or Cells[0] is null. Just put a breakpoint there and figure out which one it is.

Take off the equals:
for (int i = 0; i <= gridview.Rows.Count; i++)
Becomes
for (int i = 0; i < gridview.Rows.Count; i++)
C# arrays are zero based, but <= gives you one too many elements.
I.e. is you have 10 Rows, then you want 0..9 not 0..10
 
Share this answer
 
Comments
Am Gayathri 5-Mar-13 8:17am    
But if the count =0 it will not enter in loop correct?
Inside the loop only am adding columns.
its a problem.
Raje_ 5-Mar-13 8:28am    
for that you need to check row count before entering in loop:-
if(gridview.Rows.Count > 0)
{
//your code.
}
else
{
//your code
}
Am Gayathri 5-Mar-13 8:42am    
Thanks a lot
Hi Kavithale

You have to remove the '=' in the for loop.

b'cos "i" value is starting from 0. for example, if u have 10 records in a gridview. you have to consider 0 to 9

C#
for (int i = 0; i < gridview.Rows.Count; i++)
{
    if (!xxx.equals(gridview.Rows[i].Cells[0].Text))
    {
        dr = dt.NewRow();
        dr[0] = */adding value*/
        dt.Rows.Add(dr);
    }
}
 
Share this answer
 
Comments
Am Gayathri 5-Mar-13 8:17am    
But if the count =0 it will not enter in loop correct?
Inside the loop only am adding columns.
its a problem.
OriginalGriff 5-Mar-13 9:32am    
If the count is zero, and your test is greater-than-or-equal-to then it will enter the loop, yes.
At which point you will immediately get your error message on the if condition...
My suggestion is you can use foreach iterator statement to travarse gridview.
Instead of that
C#
for (int i = 0; i <= gridview.Rows.Count; i++)
{
    if (!xxx.Equals(gridview.Rows[i].Cells[0].Text))
    {
        dr = dt.NewRow();
        dr[0] = */adding value*/
        dt.Rows.Add(dr);
    }
}

You can use
C#
 foreach (GridViewRow row in gridview.Rows)
 {
    if (!xxx.Equals(row[i].Cells[0].Text))
    {
        dr = dt.NewRow();
        dr[0] = */adding value*/
        dt.Rows.Add(dr);
    }
}

It is more cleaner and readable. It will safe us for accidental errors like you faced < vs <= etc problem. I always prefer foreach to navigate list where list implement iterator pattern.
 
Share this answer
 
v2
Comments
Matt T Heffron 22-Oct-14 13:09pm    
You have a "think-o" (like a typo)...Probably an editing error:
You don't want the [i] on the if (...) line. Fixed:
if (!xxx.equals(row.Cells[0].Text))
And the xxx.equals(...) should be xxx.Equals(...) (Uppercase matters.)
S. M. Ahasan Habib 23-Oct-14 2:43am    
thanks! i updated code sample.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900