【问题标题】:Auto Compute of Inputs in DataGridView - C#DataGridView 中输入的自动计算 - C#
【发布时间】:2010-07-11 15:55:53
【问题描述】:

我只是想问问。我有一个产品数据网格视图,其中包含 ProdName、Quantity、SellingPrice 和 Total 列。 Quantity 和 SellingPrice 都将 ReadOnly 属性设置为 false。所以基本上,用户可以在运行时更改其中的值。我想知道的是如何在 Total 列中插入一个值,该值实际上是 Quantity 和 SellingPrice 的乘积?

欢迎任何意见。谢谢:)

【问题讨论】:

  • 不,这只是一个 Windows 应用程序。 :)

标签: c# datagridview autocomplete


【解决方案1】:

您可以使用CellValidated 事件:

private void dataGridView1_CellValidated(object sender, DataGridViewCellEventArgs e)
{
    // TODO: Add proper error handling by checking for null values, 
    // using decimal.TryParse, ...
    var row = dataGridView1.Rows[e.RowIndex];
    int quantity = int.Parse(row.Cells[1].Value.ToString());
    decimal sellingPrice = decimal.Parse(row.Cells[2].Value.ToString());
    row.Cells[3].Value = quantity * sellingPrice;
}

【讨论】:

  • 但是 CellValidated 会因为许多原因被触发,包括一些仅 UI 的事件,而不仅仅是当单元格值更改时!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-05-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-04
相关资源
最近更新 更多