【问题标题】:how to calculate datagridview column?如何计算datagridview列?
【发布时间】:2018-07-03 09:11:28
【问题描述】:

我是C# winform的新用户,也是本站的新用户

我有一个有 4 列的 datagridview

      No.     proname           price        qty      total

      1        fish               0.0         1         -
      2        tofu               0.0         1         -

像这样的例子中的数据,我给了默认价格,上面的数量是空的。现在我需要将列价格上的cellclick 设置为当前行上的新输入值,在我给出新值后,我按 Enter 键离开它,或者无论如何只留下我需要当前行上的price*qty = total 的单元格。或者相反,我将数量新输入为 2... 并且价格不是“0.0”,然后是 Price * qty=total

但如果我在其他单元格上进行编辑,则上述事件中的两个都不会执行任何操作。

如何乘以?

谁能帮我找到更好的解决方案?

提前谢谢你。

【问题讨论】:

标签: c#


【解决方案1】:

在代码后面使用这个

int.Parse(row.Cells[3].Value.toString()) * int.Parse(row.Cells[4].Value.toString())

你的方法看起来像这样

private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
    {
        int quantity,rate;
        if (int.TryParse(dataGridView1.Rows[e.RowIndex].Cells["quantity"].Value.ToString(), out quantity) && int.TryParse(dataGridView1.Rows[e.RowIndex].Cells["rate"].Value.ToString(), out rate))
        {
            int price = quantity * rate;
            dataGridView1.Rows[e.RowIndex].Cells["price"].Value = price.ToString();
         }
    }

For more find reference here

Another Reference

【讨论】:

    【解决方案2】:

    这正是您正在寻找的。 Answer

    这家伙给出的答案解释得很好,实现起来并不难。

    通过这种方式,“总计”列通过 表示您不能手动对该列执行任何操作的表达式。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多