Nevron Open Vision Documentation
Calculated Columns

 
About Calculated Columns

Unlike  Data Columns, calculated columns do not show a specific data source field. Instead it is a user responsibility to define a value for each data source row. Typically this value is a product of other data source fields. For example the TOTAL column in many grids is derived by multiplying the PRICE * QUANTITY values. Another scenario where calculated columns may be useful is if you want to extract information about a specific data row from another data source - for example pull additional information about a person, product etc.

There are two types of calculated columns:

Both types of calculated columns derive from the NCalculatedColumn base class. Calculated columns cannot be edited and that is why by default the Editor of calculated columns is null. Even if you assign an editor to a calculated column, it will raise an exception, when the user attempts to set a row value.

The rest of the topic discusses the types of calculated columns: 

NFormulaCalculatedColumn

A formula calculated column is represented by the NFormulaCalculatedColumn class. This type of column assigns a row value that is the product of a formula evaluation. The formula is evaluated each time the grid requests a value for a specific row. The formula is usually referencing other row values. The following code example demonstrates the formula calculated column: 

Formula Calculated Column Example
Copy Code
// create a dummy data table
NMemoryDataTable dataTable = new NMemoryDataTable(new NFieldInfo[]{
    new NFieldInfo("Product", typeof(string)),
    new NFieldInfo("Price", typeof(double)),
    new NFieldInfo("Quantity", typeof(int)),
});
dataTable.AddRow("Dingtincof", 100.9, 4);
dataTable.AddRow("Conremdox", 289.9, 3);
dataTable.AddRow("Sonlight", 158.9, 3);
// bind the grid to data
grid.DataSource = new NDataSource(dataTable);
// create a total column that is calculated as Price * Quantity
NFormulaCalculatedColumn totalColumn = new NFormulaCalculatedColumn();
totalColumn.Title = "Total";
totalColumn.Formula = grid.CreateFormulaFieldName("Price") + "*" + grid.CreateFormulaFieldName("Quantity");
grid.Columns.Add(totalColumn);

The row value type associated with formula calculated columns is NVariant, since the result of each formula evaluation is a variant. The format is hence set to NVariantColumnFormat

NCustomCalculatedColumn

A custom calculated column is represented by the NCustomCalculatedColumn<TRowValue> generic class. This type of column invokes a delegate function each time needs to provide a row value. The delegate is specified by the GetRowValueDelegate field. The following code example demonstrates the custom calculated column:

NCustomCalculatedColumn
Copy Code
// create a dummy data table
NMemoryDataTable dataTable = new NMemoryDataTable(new NFieldInfo[]{
    new NFieldInfo("Product", typeof(string)),
    new NFieldInfo("Price", typeof(double)),
    new NFieldInfo("Quantity", typeof(int)),
});
dataTable.AddRow("Dingtincof", 100.9, 4);
dataTable.AddRow("Conremdox", 289.9, 3);
dataTable.AddRow("Sonlight", 158.9, 3);
// bind the grid to data
grid.DataSource = new NDataSource(dataTable);
// create a total column that is calculated as Price * Quantity
NCustomCalculatedColumn<double> totalColumn = new NCustomCalculatedColumn<double>();
totalColumn.Title = "Total";
totalColumn.GetRowValueDelegate = delegate(NCustomCalculatedColumnGetRowValueArgs<double> args) 
{
    double price = Convert.ToDouble(args.DataSource.GetValue(args.RowIndex, "Price"));
    int quantity = Convert.ToInt32(args.DataSource.GetValue(args.RowIndex, "Quantity"));
    return (double)(price * quantity);
};
grid.Columns.Add(totalColumn);

The row value type associated with formula calculated columns is the TRowValue generic argument of the NCustomCalculatedColumn<TRowValue> class. Respectively the format associated with the column also is chosen to match the TRowValue generic argument.

 

Send Feedback