这是我的datagridview上的一些数据,数据就像这样
column a | column b
1 0
2 is = 2-1
3 is = 3-2
4 is = 4-3
所以,我想知道如何在a栏中计算并在b栏中显示答案。
答案 0 :(得分:0)
试试这个:
private void button1_Click(object sender, EventArgs e)
{
//skip last emtpy line(if don't have, remove -1)
for (int i = 1; i < dataGridView1.Rows.Count - 1; i++)
{
int valueCellIndex = 0;//assume calculate the 0th column
int resultCellIndex = 1;//assume result to put into the 1th column
//assume values are integers
var lastRow = dataGridView1.Rows[i - 1];
var curRow = dataGridView1.Rows[i];
int last = Convert.ToInt32(lastRow.Cells[valueCellIndex].Value);
int cur = Convert.ToInt32(curRow.Cells[valueCellIndex].Value);
curRow.Cells[resultCellIndex].Value = cur - last;
}
}