Question

What I need to do is calculated the value of one field in the grid, based on the values of other fields in the grid. I need to run this calculation After the value in one of the dependent cells is changed, but only if the value was a Valid entry. The EditValueChanged, Validating, and Validated events of the editor/repository all occur before the data is posted back into the datasource. I am wondering if there is any event I can hook into that will allow me to fire this calculation after the data has been post back into the datasource, but before control is returned to the user.

Sample Code

//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;
        }
    }
}

the problem is that everywhere I call this from, and whether I use vwDDJournal.GetRowCellValue(...) or vwDDJournal.GetFocusedRow() as MyObject, I still get the old edit value.

Requirements

I have to have the input validated before running the calculation. I have to run the calculation immediately after making the change.

Was it helpful?

Solution

... What I need to do is calculated the value of one field in the grid, based on the values of other fields in the grid.

The best way to accomplish this task is using Unbound Columns feature.

The following example demonstrates how to implement this feature by handling the ColumnView.CustomUnboundColumnData event:

// 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);
}

Original example: How to: Add an Unbound Column Storing Arbitrary Data

You can also implement calculated value for unbound column using expressions:

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);

OTHER TIPS

How about CustomCellValue?

After posting back to the data source refresh the data.

It's called whenever data is updated or view is changed.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top