JAS*_*JAS 5 c# datagridview date-arithmetic
我有一个带有Datagridview的窗体,其中包含来自xml文件的数据.DGV的设置如下:日期,产品,价格
有10行数据.
我试图计算价格从一行到下一行的变化,并且很难找到解决方案.例如:
1/1/12, ABC, $2.00
1/1/12, ABC, $2.50
Net Change: .50
Run Code Online (Sandbox Code Playgroud)
在列中,我可以使用Table.Columns.Expression,但我不清楚如何将此计算减去先前与当前.
我想过迭代列,添加到一个新表并以这种方式计算,但似乎是一个低于标准的解决方案.一旦我得到更改ID,就像将其添加到Datagridview.
没有经过测试,但是这样的话:
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
}
Run Code Online (Sandbox Code Playgroud)
为什么不做类似的事情(未经测试):
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);
Run Code Online (Sandbox Code Playgroud)
或者更好的是,在第一次从文件导入数据并插入行时计算它。