This question has already been answered:
Amount by column in dataGridView.
Please help me with this problem. Fill dataGridView3 in this way:
private void button5_Click(object sender, EventArgs e) { //Загружаем таблицу из теплосетей OleDbConnection _connection = new OleDbConnection(); StringBuilder ConnectionString = new StringBuilder(""); ConnectionString.Append(@"Provider=Microsoft.Jet.OLEDB.4.0;"); ConnectionString.Append(@"Extended Properties=Paradox 5.x;"); ConnectionString.Append(@"Data Source=C:\Teplo\TepSeti\;"); _connection.ConnectionString = ConnectionString.ToString(); try { _connection.Open(); } catch { MessageBox.Show("Error openning database! "); } OleDbDataAdapter adapter = new OleDbDataAdapter("SELECT * FROM Tepotp", _connection); adapter.Fill(dataset5); _connection.Close(); // Загружаем таблицу из ОИК "Тепло" if (dataset5.Tables.Count == 0) { MessageBox.Show("Ошибка, результат не содежит строк"); return; } dataGridView3.AutoGenerateColumns = true; bindingSource5.DataSource = dataset5.Tables[0]; dataGridView3.DataSource = bindingSource5; }
Now I want to calculate the amount of the column and put in the label in this way:
foreach (DataGridViewRow row in dataGridView3.Rows) { int partialsum = 0 ; bool tryParse = int.TryParse(row.Cells[2].Value.ToString() ,out partialsum ); if (tryParse) { sum += partialsum; //row.Cells[2] - это колонка с числовыми значениями } } label6.Text = sum.ToString();
But there is an error:
The object reference does not indicate an object instance.
I can not understand why.