事件位于网格中ActiveEditor的DataSource Update之后

本文关键字:ActiveEditor DataSource Update 之后 于网格 网格 事件 | 更新日期: 2023-09-27 17:59:38

我需要做的是根据网格中其他字段的值计算网格中一个字段的值。我需要在其中一个相关单元格中的值更改后运行此计算,但前提是该值为有效条目。编辑器/存储库的EditValueChangedValidatingValidated事件都发生在数据发布回数据源之前。我想知道是否有任何事件可以让我在数据发布回数据源之后,但在控制权返回给用户之前,触发这个计算。

样本代码

//calculation functions
private void SetCalcROP(MyObjectt Row)
{
    //rop = m/hr
    TimeSpan ts = Row.ToTime - Row.FromTime;
    double diffDepth = Row.EndDepth - Row.StartDepth;
    if (ts.TotalHours > 0)//donot divide by 0
        Row.ROP = diffDepth / ts.TotalHours;
    else
        Row.ROP = 0;
}
private void SetCalcDeltaP(MyObject Row)
{
    Row.DeltaPress = Row.SPPOnBtm - Row.SPPOffBtm;
}
//events
private void repNumberInput_Validated(object sender, EventArgs e) //is actaully ActiveEditor_Validated
{
    if (vwDDJournal.FocusedColumn.Equals(colSPPOff) || vwDDJournal.FocusedColumn.Equals(colSPPOn))
        SetCalcDeltaP(vwDDJournal.GetFocusedRow() as MyObject);
}
private void repNumberInput_NoNulls_Validated(object sender, EventArgs e) //is actaully ActiveEditor_Validated
{
    if (vwDDJournal.FocusedColumn.Equals(colStartDepth) || vwDDJournal.FocusedColumn.Equals(colEndDepth))
        SetCalcROP(vwDDJournal.GetFocusedRow() as MyObject);
}
private void repTimeEdit_Validated(object sender, EventArgs e) //is actaully ActiveEditor_Validated
{
    SetCalcROP(vwDDJournal.GetFocusedRow() as MyObject);
}
private void repNumberInput_NoNulls_Validating(object sender, System.ComponentModel.CancelEventArgs e)
{
    TextEdit TE = sender as TextEdit;
    //null is not valid for this entry;
    if (string.IsNullOrEmpty(TE.Text))
    {
        e.Cancel = true;
        vwDDJournal.SetColumnError(vwDDJournal.FocusedColumn, "This Column may not be blank");
        return;
    }
    else
    {
        double tmp;
        if (!Double.TryParse(TE.Text, out tmp))
        {
            e.Cancel = true;
            vwDDJournal.SetColumnError(vwDDJournal.FocusedColumn, "This Column must contain a number");
            return;
        }
    }
}
private void repNumberInput_Validating(object sender, System.ComponentModel.CancelEventArgs e)
{
    TextEdit TE = sender as TextEdit;
    //null is not valid for this entry;
    if (!string.IsNullOrEmpty(TE.Text))
    {
        double tmp;
        if (!Double.TryParse(TE.Text, out tmp))
        {
            e.Cancel = true;
            vwDDJournal.SetColumnError(vwDDJournal.FocusedColumn, "This Column must contain a number");
            return;
        }
    }
}
private void repTimeEdit_Validating(object sender, System.ComponentModel.CancelEventArgs e)
{
    if (vwDDJournal.FocusedColumn.Equals(colToTime))
    {//dont bother to check from time
        //TIME TRAVEL CHECK!!!!
        DateTime FromTime = Convert.ToDateTime(vwDDJournal.GetRowCellValue(vwDDJournal.FocusedRowHandle, colFromTime));
        TimeEdit te = sender as TimeEdit;
        DateTime ToTime = Convert.ToDateTime(te.EditValue);
        if (ToTime < FromTime)
        {//TIME TRAVEL
            e.Cancel = true;
            vwDDJournal.SetColumnError(vwDDJournal.FocusedColumn, "To Time must be greater than From Time");
            return;
        }
    }
}

问题是,无论我在哪里调用它,无论我使用vwDDJournal.GetRowCellValue(...)还是vwDDJournal.GetFocusedRow() as MyObject,我仍然会得到旧的编辑值。

要求

在运行计算之前,我必须对输入进行验证。更改后我必须立即进行计算。

事件位于网格中ActiveEditor的DataSource Update之后

我需要做的是根据网格中其他字段的值计算网格中一个字段的值。

完成此任务的最佳方法是使用"取消绑定列"功能。

以下示例演示如何通过处理ColumnView.CustomUnboundColumnData事件来实现此功能:

// Provides data for the Total column.
void gridView1_CustomUnboundColumnData(object sender, CustomColumnDataEventArgs e) {
   if (e.Column.FieldName == "Total" && e.IsGetData) e.Value = 
     getTotalValue(e.ListSourceRowIndex);
}
// Returns the total amount for a specific row.
decimal getTotalValue(int listSourceRowIndex) {
    DataRow row = nwindDataSet.Tables["Order Details"].Rows[listSourceRowIndex];
    decimal unitPrice = Convert.ToDecimal(row["UnitPrice"]);
    decimal quantity = Convert.ToDecimal(row["Quantity"]);
    decimal discount = Convert.ToDecimal(row["Discount"]);
    return unitPrice * quantity * (1 - discount);
}

原始示例:如何:添加存储任意数据的未绑定列

您还可以使用表达式实现未绑定列的计算值:

GridColumn columnTotal = new GridColumn();
columnTotal.FieldName = "Total";
columnTotal.Caption = "Total";
columnTotal.UnboundType = DevExpress.Data.UnboundColumnType.Decimal;
columnTotal.UnboundExpression = "[Quantity] * [UnitPrice] * (1 - [Discount])";
gridView1.Columns.Add(columnTotal);

CustomCellValue怎么样?

发布回数据源后,刷新数据。

每当更新数据或更改视图时都会调用它。