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
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;
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;
}
}
}
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
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