these code work with me to sum one column in datagrideview but i want to sum multi columns in datagrideview
Are you sure?
You want to sum the first and third column , but you refer to
Cells[4]
and
Cells[5]
sum1 += Convert.ToDecimal(dgw.Rows[i].Cells[4].Value);
sum2 += Convert.ToDecimal(dgw.Rows[i].Cells[5].Value);
Also, the way you add new row is wrong:
string[] totalrow = new string[] { sum1.ToString(), "", sum2.ToString() };
The size of array have to be equal to the count of cells!
For example:
dataGridView1.Columns[0].Name = "Recipe";
dataGridView1.Columns[1].Name = "Category";
dataGridView1.Columns[2].Name = "Main Ingredients";
dataGridView1.Columns[3].Name = "Rating";
string[] row1 = new string[] { "Meatloaf", "Main Dish", "ground beef", "**" };
dataGridView1.Rows.Add(row1);
For further details, please see:
DataGridView.Rows Property (System.Windows.Forms)[
^]