Click here to Skip to main content
15,920,603 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Does anyone please know how to add the sum of items in a datagridview
Posted
Comments
George Jonsson 30-Nov-15 2:05am    
Is this a windows forms project?
And do you want to sum up column wise or row wise?

 
Share this answer
 
Comments
George Jonsson 30-Nov-15 2:05am    
Not entirely sure, but the OP asks for a DataGridView which implies windows forms.
 
Share this answer
 
Comments
George Jonsson 30-Nov-15 2:04am    
Not entirely sure, but the OP asks for a DataGridView which implies windows forms.
You can do it by many ways.
C#
int sum = 0;
for (int i = 0; i < dataGridView1.Rows.Count; ++i)
{
    sum += Convert.ToInt32(dataGridView1.Rows[i].Cells[1].Value);
}


You can use Linq
C#
int sum= dataGridView1.Rows.Cast<datagridviewrow>()
                .Sum(t => Convert.ToInt32(t.Cells[1].Value));


and finally you can add a Row on your gridview or you can show sum result in Label .
 
Share this answer
 
v2

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