Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how I can show the sum of in a datagridview column?

I need to show the sum of the count column from this datagridview, but I don't know how I can get to the data in the datagridview.

When I click on the button, I want to show 94 in label1.

How can this be done?

alt text

like image 614
mahnaz Avatar asked Sep 23 '10 15:09

mahnaz


People also ask

How do you display the sum of values in the cells of last column in DataGridView?

Cast<DataGridViewRow>(). Sum(x => { double val = 0; if (double. TryParse(x. Cells["Column"], out val)) return val; else return 0; });

How many rows can DataGridView handle?

Max value, i.e. 2,147,483,647. The DataGridView's RowCount cannot hold a number more than this because it's an integer. So, you can possibly say that limit for datagridview rows is 2 Billion.

Which property of DataGridView or Datatable gives the number of rows?

Use the GetRowCount and GetRowsHeight methods to determine the number of rows or the combined height of rows in a particular state.


2 Answers

int sum = 0; for (int i = 0; i < dataGridView1.Rows.Count; ++i) {     sum += Convert.ToInt32(dataGridView1.Rows[i].Cells[1].Value); } label1.Text = sum.ToString(); 
like image 110
JamesMLV Avatar answered Sep 24 '22 09:09

JamesMLV


Fast and clean way using LINQ

int total = dataGridView1.Rows.Cast<DataGridViewRow>()                 .Sum(t => Convert.ToInt32(t.Cells[1].Value)); 

verified on VS2013

like image 45
Natan Braslavski Avatar answered Sep 21 '22 09:09

Natan Braslavski