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..

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();
                        }
                    }    
                }                     
            }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.