I have a windows form with a Datagridview in it containing data from an xml file. The DGV is setup like this: Date, Product, Price
There are 10 rows of data.
Im trying to calculate the change in Price from one row to the next and having difficulty finding a solution. For example:
1/1/12, ABC, $2.00
1/1/12, ABC, $2.50
Net Change: .50
Across columns I can use a Table.Columns.Expression, but it is unclear to me how I can do this calculation subtracting previous vs. current.
I thought about iterating through the columns, adding to a new table and calculating it that way, but seems like a sub-par solution. Once I get the change Id like to add that to that Datagridview.
Have 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
}
Why not just do something similar to (untested):
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);
Or better yet, calculate it the first time you import the data from the file and insert the rows.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.