更正事件以计算DataGridView中的单元格值 [英] Correct event to calculate Cells values in a DataGridView

查看:60
本文介绍了更正事件以计算DataGridView中的单元格值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在DataGridView中,我在 CellEndEdit 事件中使用此代码来自动计算数量乘以数量:

In a DataGridView I use this code, in the CellEndEdit event to automatically calculate the amounts multiplied by the quantity:

private void myGrid_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
    DataGridView oDGV = (DataGridView)sender; 
    DataGridViewCell oCell = oDGV.Rows[e.RowIndex].Cells[e.ColumnIndex];

    // Only the quantity can be changed
    if (oCell.Value != DBNull.Value && oCell.OwningColumn.Name == "num")
    {
        oDGV.EndEdit(); 
        int nNum = Convert.ToInt16(oCell.Value);
        decimal nPrice = Convert.ToDecimal(oDGV.Rows[e.RowIndex].Cells["price"].Value.ToString());
        decimal nTot = nPrice * nNum;
        oDGV.Rows[e.RowIndex].Cells["tot"].Value = nTot;
    }
}

这显然是一个简单的操作,但是它经常发生在我身上,导致不执行计算,因此我必须返回到该单元格,再次键入该值,然后按Enter键(即使屏幕上的所有内容都正确).
我不明白问题出在哪里,也许我必须使用另一个事件?

Apparently a simple operation, but it happens to me every so often that the calculation is not carried out and I have to go back to the cell, type the value again and press enter (even if everything is correct on the screen).
I don't understand where the problem is, maybe I have to use another event?

推荐答案

您有3列需要 interact :

  • price 列:应指定单价
  • num 列:应为项目数,即 Quantity
  • tot 列:由 [单价] * [数量]
  • 表示的 Total
  • price Column: it should specify a Unit Price
  • num Column: it should be the number of Items, the Quantity
  • tot Column: the Total value represented by [Unit Price] * [Quantity]

由于获得了第三列的( Total )值乘以价格( Unit Price )乘以商品的数量( Quantity ),此计算可以传输到 DataTable.Expression :此操作是固定的(它始终引用相同的列,始终具有相同的值类型).

Since the third Column's (Total) Value is obtained multiplying the price (Unit Price) by the number (Quantity) of items, this calculation can transferred to a DataTable.Expression: this operation is fixed (it always refers to the same Columns, which always have the same Value Type).

DataTable.Columns["Total"].Expression = "[Unit Price] * [Quantity]";

当然,在您的代码中,您使用分配给列的名称:

In your code, of course, you use the names you have assigned to your Columns:

[Your DataTable].Columns["tot"].Expression = "[price] * [num]";

►请注意,由于要表达价格(货币),因此可能要使用小数类型表示该值,而不是整数类型. Total 列也是如此.

► Note that, since you are expressing a Price (currency), you probably want to use a Decimal type to represent that Value, not an integer type. The same applies to the Total Column.

可以在加载DataTable之后或将其分配给 DataGridView.DataSource 属性之后立即分配表达式.
分配属性后, DataSourceChanged 事件引发:

The Expression can be assigned right after you have loaded the DataTable, or after you have assigned it to the DataGridView.DataSource property.
Assigning the property, the DataSourceChanged event is raised:

private void myGrid_DataSourceChanged(object sender, EventArgs e)
{
    if (myGrid.DataSource is DataTable dt && dt.Columns.IndexOf("tot") >= 0) {
        dt.Columns["tot"].Expression = "[num] * [price]";
    }
}

►单元格不需要包含值, [单价] [数量] 都可以为 null ,否引发异常.您当然可以使用DataGridView NewRow 向DataTable添加新的行.

► The Cells don't need to contain a value, both [Unit Price] and [Quantity] can be null, no exception is thrown. You can of course use the DataGridView NewRow to add a new Row to the DataTable.

要在更改单价 Quantity 值后立即更改 Total 列的值,请订阅 CellEndEdit CellValueChanged 事件并验证编辑.在这种情况下,您无需按Enter或更改Row即可看到新的计算值,只需将光标移至另一个相邻的Cell(Excel样式)即可.

To change the value of the Total Column right after either the Unit Price or the Quantity values are changed, subscribe to the CellEndEdit or CellValueChanged events and Validate the edit. In this case, you don't need to press Enter or change Row to see the new calculated value appear, just move the cursor to another adjacent Cell (Excel style).

private void myGrid_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
    Validate();
}

这篇关于更正事件以计算DataGridView中的单元格值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆