The purpose of the code below is to sum up all the index 3 of a datagridview
that would continue on adding endlessly in an order system and display it in a label named Price
.
private void Add_Click(object sender, EventArgs e)
{
dgvSelected.Rows.Add(dgvItem.SelectedCells[0].Value.ToString(),
dgvItem.SelectedCells[1].Value.ToString(),
dgvItem.SelectedCells[2].Value.ToString(),
dgvItem.SelectedCells[3].Value.ToString());
for (int i = 0; i < dgvSelected.Rows.Count; i++)
{
price = price + int.Parse(dgvSelected.Rows[i].Cells[3].Value.ToString());
Price.Text = price.ToString();
}
}
The problem is that it keeps saying:
"Object reference not set to an instance of an object."
...as an error for the following line:
price = price + int.Parse(dgvSelected.Rows[i].Cells[3].Value.ToString());
There is supposed to be no null in the table but the error keeps on insisting that there is a null. There are multiple rows in the table but the only row it can read is the 1st row. When it comes to the 2nd row which has values, it then will say the error concerned.