0

i have a problem in summing up all rows which is the datagridview columns are dynamic.

the datagridview goes like this:

Name Score1 Score2 Score3 total
Mark 11 + 15 + 14 = 40
John 10 + 15 + 13 = 38

please help..

2
Contributors
1
Reply
13
Views
4 Years
Discussion Span
Last Post by tinstaafl
0

Try something like this:

            dataGridView1.Rows.Add("Mark",11,15,14,0);
            dataGridView1.Rows.Add("John", 10,15,13,0);
            foreach (DataGridViewRow row in dataGridView1.Rows)
            {
                int total = 0;                                    
                foreach (DataGridViewCell c in row.Cells)
                {
                    if (c.Value != null)
                    {
                        if (c.ColumnIndex != 0 && c.ColumnIndex != 4)
                        {
                            total += Convert.ToInt16(c.Value);
                        }
                        else if (c.ColumnIndex == 4)
                        {
                            c.Value = total.ToString();
                        }
                    }    
                }                     
            }

Edited by tinstaafl

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.