Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/323.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Datagridview上的计算?_C#_Winforms_Arraylist_Datagridview - Fatal编程技术网

C# Datagridview上的计算?

C# Datagridview上的计算?,c#,winforms,arraylist,datagridview,C#,Winforms,Arraylist,Datagridview,这是我的datagridview上的一些数据,数据如下 column a | column b 1 0 2 is = 2-1 3 is = 3-2 4 is = 4-3 因此,我想知道如何在a列中计算,并在b列中显示答案。试试这个: private void button1_Click(object sender, EventArgs e) { //skip last emtp

这是我的datagridview上的一些数据,数据如下

column a | column b
1             0
2          is = 2-1
3          is = 3-2
4          is = 4-3
因此,我想知道如何在a列中计算,并在b列中显示答案。

试试这个:

        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;
        }
    }
private void按钮1\u单击(对象发送者,事件参数e)
{
//跳过最后一行emtpy(如果没有,请删除-1)
对于(int i=1;i
您需要第3列中的计算值吗?@Krishna,OP提到b列应该包含计算值。我想知道怎样才能做到这一点datagridview@uɐpuɐɥƆ如何仅使用一个值进行计算?您需要2才能正确计算?计算是否假定为a列当前行的
值减去a列上一行的
值?还是像问题中那样硬编码?@Krishna-是的,你是对的。OP要在a列中计算(第2行减去第1行),(第3行减去第2行)…等等。希望在列中显示的结果-bI已尝试过。但它表示“无法将'System.Windows.Forms.DataGridViewTextBoxCell'类型的对象强制转换为'System.IConvertible'类型。”