C # Datagridview Row Calculation

I have a window form with a Datagridview containing data from an XML file. DGV is configured as follows: Date, Product, Price

There are 10 rows of data.

I am trying to calculate the price change from one line to another and having difficulty finding a solution. For instance:

1/1/12, ABC, $2.00
1/1/12, ABC, $2.50

Net Change: .50

In all columns I can use Table.Columns.Expression, but I don’t understand how I can do this calculation by subtracting the previous against the current.

I thought about iterating over the columns, adding to a new table and computing it that way, but it seems like a subparallel solution. As soon as I get the change identifier, I would like to add this to this Datagridview.

Not tested, but something like this:

if (dataGridView1.Rows.Count > 0)
{
    specifier = "###,###.00";

    double tier1Minus = 0;
    double tier2Minus = 0;

    for (int i = 0; i < dataGridView1.Rows.Count; ++i)
    {
        tier1Minus += Convert.ToDouble(dataGridView1.Rows[i].Cells["Price"].Value);

        tier2Minus += Convert.ToDouble(dataGridView1.Rows[i+1].Cells["Price"].Value);
    }

    // then add to dataGridView1
}
+5
source share
1 answer

- ():

for(int i = 0; i < dataGridView1.Rows.Count - 1; i++)
    dataGridView1.Rows[i+1].Cells["NetChange"].Value =
        Convert.ToDouble(dataGridView1.Rows[i+1].Cells["Price"].Value) - 
        Convert.ToDouble(dataGridView1.Rows[i].Cells["Price"].Value);

, .

+3