我的任务是,我有两个有界列(DiscountPercentage和DiscountAmount)。如果我在DiscountPercentage中更改值自动计算和显示的DiscountAmount和相同如果我更改DiscountAmount我需要计算DiscountPercentage。所以我创建了两个无界列(百分比,数量)我通过Unbound Expression计算。但我不会设置如果我点击键盘上的“标签”按钮。然后我创建了“FocusedRowchanged”事件并在那里编写代码以在Bounded列中设置值。当我进入下一行时它会设置。但我需要立即设置/更新当前单元格到下一个单元格。
我的代码是
private void gridView1_CellValueChanged(object sender, DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs e)
{
// % to amt
object obj = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, gridView1.Columns["DiscountPercentage"]) == DBNull.Value
? 0
: (gridView1.GetRowCellValue(gridView1.FocusedRowHandle, gridView1.Columns["DiscountPercentage"]));
decimal a = Convert.ToDecimal(obj);
if (a > 0)
{
percentage.UnboundType = DevExpress.Data.UnboundColumnType.Decimal;
percentage.UnboundExpression = "[UnitPrice] * ([DiscountPercentage] / 100.0)";
}
// amt to %
object dm = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, gridView1.Columns["DiscountAmount"]) == DBNull.Value
? 0
: (gridView1.GetRowCellValue(gridView1.FocusedRowHandle, gridView1.Columns["DiscountAmount"]));
decimal a1 = Convert.ToDecimal(dm);
if (a1 > 0)
{
amount.UnboundType = DevExpress.Data.UnboundColumnType.Decimal;
amount.UnboundExpression = "Round(([DiscountAmount] / [UnitPrice]) * 100.0, 2)";
}
}
private void gridView1_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
{
// % to amt
object obj = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, gridView1.Columns["DiscountPercentage"]) == DBNull.Value
? 0
: (gridView1.GetRowCellValue(gridView1.FocusedRowHandle, gridView1.Columns["DiscountPercentage"]));
decimal a = Convert.ToDecimal(obj);
if (a > 0)
{
object obj2 = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, gridView1.Columns["gridColumn1"]);
int aa = Convert.ToInt32(obj2);
gridView1.SetRowCellValue(gridView1.FocusedRowHandle, gridView1.Columns["DiscountAmount"], aa);
}
// amt to %
object dm = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, gridView1.Columns["DiscountAmount"]) == DBNull.Value
? 0
: (gridView1.GetRowCellValue(gridView1.FocusedRowHandle, gridView1.Columns["DiscountAmount"]));
decimal a1 = Convert.ToDecimal(dm);
if (a1 > 0)
{
object obj3 = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, gridView1.Columns["gridColumn2"]);
int aa2 = Convert.ToInt32(obj3);
gridView1.SetRowCellValue(gridView1.FocusedRowHandle, gridView1.Columns["DiscountPercentage"], aa2);
}
}
例如,如果Price = 1000 DiscountPercentage = 5则自动将DiscountAmount设置为50 DiscountAmount = 100然后自动将DiscountPercentage设置为10
我需要在同一行执行此操作吗?如何完成我的任务?在此先感谢。
答案 0 :(得分:2)
您可以在不使用未绑定列的情况下执行此操作,这是一个示例(我已经测试过它并且工作正常)
private void gridView1_CellValueChanged(object sender, CellValueChangedEventArgs e)
{
var row = gridView1.GetFocusedDataRow();
// Calculating the dicsount %
if (e.Column == colDiscountAmout)
{
var productPrice = Convert.ToDecimal(row["Price"]);
var discountAmout = Convert.ToDecimal(row["DiscountAmout"]);
row["DiscountPercent"] = (discountAmout * 100) / productPrice;
}
// Calculating the discount amount
if (e.Column == colDiscountPercent)
{
var productPrice = Convert.ToDecimal(row["Price"]);
var discountPercent = Convert.ToDecimal(row["DiscountAmout"]);
row["DiscountAmout"] = productPrice * (discountPercent / 100);
}
}
答案 1 :(得分:1)
在.AcceptChanges()
事件期间尝试在基础数据源上调用DataGridView.CellEndEdit
。