Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
I want to know I have 2 columns in data grid view and now i want to show total of columns in data grid view last row how can do this please help me and give me best suggestion with example

What I have tried:

int sum = 0;
for (int i = 0; i < dataGridView1.Rows.Count; ++i)
{
sum += Convert.ToInt32(dataGridView1.Rows[i].Cells[1].Value);
}
textBox1.Text = sum.ToString();
int sum1 = 0;
for (int i = 0; i < dataGridView1.Rows.Count; ++i)
{
sum1 += Convert.ToInt32(dataGridView1.Rows[i].Cells[2].Value);
}
textBox3.Text = sum1.ToString();
Posted
Updated 31-Aug-16 5:47am

1 solution

You will need to add another row after doing the calculations, unless its is already there (which if it is hopefully it is zeroed whenever you try to do this adding).

C#
dataGridView1.Rows.Add();


Then you just need to set the value of each cell in the last row.

C#
int rowIdx = dataGridView1.Rows.Count - 1;
dataGridView1.Rows[rowIdx].Cells[1].Value = sum;
dataGridView1.Rows[rowIdx].Cells[2].Value = sum1;
 
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