DevExpress GridView单元格CellValueChanged事件详解
作者:C/S框架网|www.cscode.ne  发布日期:2020-03-22 08:07:25
  DevExpress GridView单元格CellValueChanged事件详解

DevExpress GridView单元格CellValueChanged事件详解


GridView的CellValueChanged事件:当用户修改单元格的值时立即触发。

CellValueChangedEventHandler事件描述:Fires immediately after a cell's value has been changed.

C# Code:

//
// 摘要:
// Fires immediately after a cell's value has been changed.
[DevExpressXtraGridLocalizedDescriptionAttribute("ColumnViewCellValueChanged")]
[DXCategory(
"Property Changed")]
public event CellValueChangedEventHandler CellValueChanged;



开发框架应用场景:

开发企业管理系统比如ERP系统的采购订单,明细表通常有采购数量、单价、金额等字段。当用户修改数量或单价时,要自动计算金额。

金额字段是只读的,不可直接修改。


C/S开发框架参考代码:

C# Code:

gvDetail.CellValueChanged += new DevExpress.XtraGrid.Views.Base.CellValueChangedEventHandler(OnCellValueChanged); //表格值改变

//来源:C/S框架网 | www.csframework.com | QQ:23404761



OnCellValueChanged事件:


C# Code:

private void OnCellValueChanged(object sender, CellValueChangedEventArgs e)
{
  
if ((e.Column == colD_Price) || (e.Column == colD_Quantity))
  {
    
decimal price = ConvertEx.ToDecimal(gvDetail.GetDataRow(gvDetail.FocusedRowHandle)[tb_POs.Price]);//单价
    
decimal quantity = ConvertEx.ToDecimal(gvDetail.GetDataRow(gvDetail.FocusedRowHandle)[tb_POs.Quantity]);//数量
    
decimal amt = Math.Round(price * quantity, 2, MidpointRounding.ToEven);//金额=数量*单价
    
    
//计算本产品的采购金额
    
gvDetail.SetFocusedRowCellValue(colD_Amount, amt);
    
    gvDetail.UpdateCurrentRow();
//更新当前资料行
    
gvDetail.UpdateTotalSummary();//更新合计
    
    
//更新主表的合计金额
    
decimal totalAmt = ConvertEx.ToDecimal(colD_Amount.SummaryItem.SummaryValue);
    
this.SetEditorBindingValue(txtAmount, totalAmt, true);
  }
}

//来源:C/S框架网 | www.csframework.com | QQ:23404761



OnCellValueChanged事件的CellValueChangedEventArgs参数:


C# Code:

public class CellValueChangedEventArgs : EventArgs
{
  DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs.Column
  DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs.Value
  
  
public CellValueChangedEventArgs(int rowHandle, GridColumn column, object value);
  
  
//
  
// 摘要:
  
// Gets the column that contains the processed cell.
  
public GridColumn Column { get; }
  
//
  
// 摘要:
  
// Gets the handle of the row that contains the processed cell.
  
public int RowHandle { get; }
  
//
  
// 摘要:
  
// Gets the current cell value.
  
public object Value { get; }
}

//来源:C/S框架网 | www.csframework.com | QQ:23404761



通过此参数我们可以获取当前修改的列对象、资料行号以及单元格的值。


C/S框架网|原创精神.创造价值.打造精品

扫一扫加微信:
C/S框架网作者微信 C/S框架网|原创作品.质量保障.竭诚为您服务


上一篇 下一篇