我正在使用 RadGridView
来显示正在出售的商品。在同一行我还有一个“数量”、“单价”、“总价”列。
当用户更改“数量”列的值时,我想触发一个事件,通过将“数量”乘以“商品价格”来计算“总价”列的值
如何添加这样一个只有在“数量”列的值发生变化时才会触发的事件?
这个我试过了,没有影响
private void radGridView1_CurrentRowChanged(object sender, CurrentRowChangedEventArgs e) {
double itemPrice = Convert.ToDouble(e.CurrentRow.Cells["Unit Price"].Value);
int itemQty = Convert.ToInt32(e.CurrentRow.Cells["Qty"].Value);
double totalPrice = itemPrice * itemQty;
e.CurrentRow.Cells["Total Price"].Value = totalPrice.ToString();
}
最佳答案
订阅 CellEndEdit
事件(如果需要,在您的构造函数中):
radGridView1.CellEndEdit += (s, e) =>
{
if (e.Column == radGridView1.Columns["Qty"])
{
var row = radGridView1.CurrentRow.Cells;
row["Total Price"].Value =
(int)row["Qty"].Value * (decimal)row["Item Price"].Value;
}
};
您可能想要添加一些错误处理,并在价格不是小数时转换为不同的类型,等等。
你也可以把它拆分成一个单独的方法;使用简短的方法,我有时会发现这种“内联”方式更易于阅读和维护。 YMMV.
关于c# - 如何在 RadGridView 中的单元格值更改时触发事件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28132848/