Problem when summing datagridview selected cells

Nov 13 2010 8:43 AM

Hi guys

I to label (tlblPriceTotal) show sum of all selected cells.

I programed this code

But it does not works correctly. when i selecte something this code begin summing from first row

if i select another column it summing same (priceDataGridViewTextBoxColumn)

what is problem can anybody help me?

Thanks.

 private void dataGridView1_SelectionChanged(object sender, EventArgs e)
{
decimal nextNumber = 0;
decimal sumNumbers = 0;

for (int i = 0; i < dataGridView1.SelectedCells.Count - 1; i++)
{
nextNumber = (decimal)dataGridView1.Rows[i].Cells["priceDataGridViewTextBoxColumn"].Value;
sumNumbers += nextNumber;
}

tlblPriceTotal.Text = "Sum: " + sumNumbers.ToString();
tlblSelectedCellsTotal.Text = "Total Selected: " + dataGridView1.SelectedCells.Count.ToString();
}



Answers (2)