【问题标题】:Calculation on Datagridview?在 Datagridview 上计算?
【发布时间】:2017-07-06 14:17:42
【问题描述】:

这是我的datagridview上的一些数据,一个数据就是这样的

column a | column b
1             0
2          is = 2-1
3          is = 3-2
4          is = 4-3

所以,我想知道如何在 a 列中计算并在 b 列中显示答案。

【问题讨论】:

  • 你需要计算第三列的值吗?
  • @Krishna,OP 提到 b 列应包含计算值。 Op 想要如何在 datagridview 中做到这一点
  • @uɐpuɐɥƆ 你如何计算只有一个值,你需要 2 来计算正确的?
  • 计算应该是value of current row in column a minus value of previous row in column a吗?还是像问题中那样硬编码?
  • @Krishna - 是的,你是对的。 OP 想在 col-a 中计算(第 2 行减去第 1 行)、(第 3 行减去第 2 行)...等等。 OP想要在列中显示的结果 - b

标签: c# winforms arraylist datagridview


【解决方案1】:

试试这个:

        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;
        }
    }

【讨论】:

  • 我试过了。但它说“无法将'System.Windows.Forms.DataGridViewTextBoxCell'类型的对象转换为'System.IConvertible'。'”
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-06-23
  • 2021-05-16
  • 1970-01-01
相关资源
最近更新 更多