更正事件以计算 DataGridView 中的单元格值 [英] Correct event to calculate Cells values in a 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;
}
}
显然是一个简单的操作,但我经常遇到计算未执行的情况,我必须返回单元格,再次输入值并按回车键(即使屏幕上的一切都是正确的).
我不明白问题出在哪里,也许我必须使用另一个事件?
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 列需要交互:
price
列:它应该指定一个Unit Price
num
列:应该是Item的数量,Quantity
tot
列:[Unit Price] * [Quantity]
表示的
Total
值price
Column: it should specify aUnit Price
num
Column: it should be the number of Items, theQuantity
tot
Column: theTotal
value represented by[Unit Price] * [Quantity]
由于第三列的 (Total
) 值是将价格 (Unit Price
) 乘以商品数量 (Quantity
) 获得的,此计算可以转移到 DataTable.Expression:这个操作是固定的(它总是引用相同的Columns,它们总是具有相同的Value Type).
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]";
▶ 请注意,由于您要表示价格(货币),您可能希望使用 Decimal 类型来表示该值,而不是整数类型.这同样适用于 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.
可以在加载数据表后立即分配表达式,或者在将其分配给 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]";
}
}
▶ Cells 不需要包含值,[Unit Price]
和 [Quantity]
都可以是 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.
要在 Unit Price
或 Quantity
值更改后立即更改 Total
列的值,请订阅 CellEndEdit
或 CellValueChanged
事件并验证编辑.在这种情况下,您无需按 Enter 或更改 Row 即可看到新的计算值出现,只需将光标移动到另一个相邻的单元格(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屋!