Click here to Skip to main content
15,886,518 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
Good day, please i'm having loop issues with a gridview table. i have a gridview table like this
+--------------+---------+---------+---------+---------+---------+---------+
| Values       | Col1    | Col2    | Col3    | Col4    | Col5    | Col6    |
+--------------+---------+---------+---------+---------+---------+---------+
| Row1         | 1, 2    | 1, 2    | 1, 2    | 1, 2    | 1, 2    | 1, 2    |
+--------------+---------+---------+---------+---------+---------+---------+
| Row2         | 1, 2    | 1, 2    | 1, 2    | 1, 2    | 1, 2    | 1, 2    |
+--------------+---------+---------+---------+---------+---------+---------+
| Row3         | 1, 2    | 1, 2    | 1, 2    | 1, 2    | 1, 2    | 1, 2    |
+--------------+---------+---------+---------+---------+---------+---------+
| Row4         | 1, 2    | 1, 2    | 1, 2    | 1, 2    | 1, 2    | 1, 2    |
+--------------+---------+---------+---------+---------+---------+---------+

I want to loop through all the cells with numbers and add the numbers to give me a table that looks like this

+--------------+---------+---------+---------+---------+---------+---------+
| Values       | Col1    | Col2    | Col3    | Col4    | Col5    | Col6    |
+--------------+---------+---------+---------+---------+---------+---------+
| Row1         | 3       | 3       | 3       | 3       | 3       | 3       |
+--------------+---------+---------+---------+---------+---------+---------+
| Row2         | 3       | 3       | 3       | 3       | 3       | 3       |
+--------------+---------+---------+---------+---------+---------+---------+
| Row3         | 3       | 3       | 3       | 3       | 3       | 3       |
+--------------+---------+---------+---------+---------+---------+---------+
| Row4         | 3       | 3       | 3       | 3       | 3       | 3       |
+--------------+---------+---------+---------+---------+---------+---------+


This is what i have tried

C#
protected void add(object sender, EventArgs e)
        {
            char[] chars = {','};
            try
            {
                for (int j = 0; j < excel.Rows.Count; j++)
                {

                    for (int k = 1; k < excel.Rows.Count; k++)
                    {
                        string a = excel.Rows[j].Cells[k].Text; // this issues an index out of range error

                        string [] scores = a.Split(chars);

                        string ba = scores.ElementAt(0);
                        string ca = scores.ElementAt(1);

                        int b = Convert.ToInt32(ba);
                        int c = Convert.ToInt32(ca);
                            
                        int total = b + c;
                        string tot = total.ToString(); 

                        excel.Rows[j].Cells[k].Text = tot; // this assigns the cell the value of the sum
                    }                    
                }                    
            }
            catch (MySqlException myex)
            {
                error.Text = myex.Message;
            }
            catch (Exception ex)
            {
                error.Text = ex.Message;
            }
            
        }


but this gives me a result like this and issurs an index out of range exception on this line of code
C#
string a = excel.Rows[j].Cells[k].Text;


+--------------+---------+---------+---------+---------+---------+---------+
| Values       | Col1    | Col2    | Col3    | Col4    | Col5    | Col6    |
+--------------+---------+---------+---------+---------+---------+---------+
| Row1         | 3       | 3       | 3       | 3       | 3       | 3       |
+--------------+---------+---------+---------+---------+---------+---------+
| Row2         | 1, 2    | 1, 2    | 1, 2    | 1, 2    | 1, 2    | 1, 2    |
+--------------+---------+---------+---------+---------+---------+---------+
| Row3         | 1, 2    | 1, 2    | 1, 2    | 1, 2    | 1, 2    | 1, 2    |
+--------------+---------+---------+---------+---------+---------+---------+
| Row4         | 1, 2    | 1, 2    | 1, 2    | 1, 2    | 1, 2    | 1, 2    |
+--------------+---------+---------+---------+---------+---------+---------+


any assistance would be lovely. Thanks
Posted
Comments
Kenneth Haugland 21-Sep-15 5:22am    
Excel arrays start at 1 and not 0, so that's why you get the error. So you woul dhave to change the for loop from j=0 to j=1.

Both loops end with
C#
for (int j = 0; j < excel.Rows.Count; j++)
{

    for (int k = 1; k < excel.Rows.Count; k++)

but k counter have to ends with excel.Rows[j].Cells.Count or excel.Columns.Count

See:
GridView.Columns Property[^]
How to: Set GridView Web Server Control Column Width Dynamically[^]
 
Share this answer
 
v2
Comments
Wendelius 19-Sep-15 13:54pm    
Good catch!
Maciej Los 19-Sep-15 13:58pm    
Thank you, Mika.
EasyHero 20-Sep-15 7:13am    
I've just tried changing the second parameter in the for loop to k < excel.Cells .Count but I got an error. I'm using visual studio 2013 which runs a .NET framework 4.5. I dunno what or why I'm getting d error if gridview has a Cells.Count property
Maciej Los 20-Sep-15 7:20am    
2 options:
excel.Rows[j].Cells.Count
or
excel.Columns.Count
See:
GridView.Columns Property[^]
How to: Set GridView Web Server Control Column Width Dynamically[^]
EasyHero 20-Sep-15 7:34am    
Thanks a lot, I it worked perfectly, I'm sorry for my comment earlier, it lacked a thorough study background. I found applied your solution and it worked. I'll post the solution I found later for others too. Thanks
C#
foreach (GridViewRow j in gridview1.Rows)
{
    for (int k = 1; k < j.Cells.Count; k++)
    {
        string a = j.Cells[k].Text;
        string [] scores = a.Split(chars);
        string ba = scores.ElementAt(0);
        string ca = scores.ElementAt(1);
        int b = Convert.ToInt32(ba);
        int c = Convert.ToInt32(ca);
        int total = b + c;
        string tot = total.ToString(); 
        j.Cells[k].Text = tot; // this assigns the cell the value of the sum
    }                    
                
}                    
 
Share this answer
 

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