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.

Reported as a duplicate by andreycha c # Apr 12 '17 at 5:01 pm

A similar question was asked earlier and an answer has already been received. If the answers provided are not exhaustive, please ask a new question .

  • 3
    Can write in what line error? - Pavel S. Zaitsau 4:27
  • The case on the form happens - Gena_2007

2 answers 2

I think the error is somewhere here:


Try replacing with

 (row.Cells[2].Value ?? "").ToString() 

    Is the matter on the form or in ASP.NET happening?

    if the latter, see if your DataGrid has a binding after the destination source.

    • Step by step debugging do and suffer. to know which line is wrong. - Sofver