0

我需要做的是根据网格中其他字段的值计算网格中一个字段的值。我需要在一个依赖单元格中的值更改后运行此计算,但前提是该值是有效条目。编辑器/存储库的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(...)or vwDDJournal.GetFocusedRow() as MyObject,我仍然得到旧的编辑值。

要求

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

4

2 回答 2

0

怎么样CustomCellValue

回发到数据源后刷新数据。

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

于 2013-02-14T20:21:55.853 回答
0

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

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

以下示例演示了如何通过处理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);
于 2013-02-15T04:26:17.007 回答