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?
Cast<DataGridViewRow>(). Sum(x => { double val = 0; if (double. TryParse(x. Cells["Column"], out val)) return val; else return 0; });
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.
Use the GetRowCount and GetRowsHeight methods to determine the number of rows or the combined height of rows in a particular state.
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();
Fast and clean way using LINQ
int total = dataGridView1.Rows.Cast<DataGridViewRow>() .Sum(t => Convert.ToInt32(t.Cells[1].Value));
verified on VS2013
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With