Infragistics(R) NetAdvantage(R) Windows Forms
FormulaValueConverter Property
See Also  Example E-mail your feedback on this topic.
Infragistics.Win.UltraWinGrid Namespace > UltraGridColumn Class : FormulaValueConverter Property

Interface for providing custom logic for converting formula results and the formula source values.

Syntax

Visual Basic (Declaration) 
Public Property FormulaValueConverter As Infragistics.Win.CalcEngine.IFormulaValueConverter
C# 
public Infragistics.Win.CalcEngine.IFormulaValueConverter FormulaValueConverter {get; set;}

Example

Following code demonstrates the concepts behind IFormulaValueConverter interface. It implements this interface and assigns it to a column and a summary in the InitializeLayout event handler. The summary has a formula that uses the column as the source.

Visual BasicCopy Code
Imports Infragistics.Shared
Imports Infragistics.Win
Imports Infragistics.Win.UltraWinGrid
Imports Infragistics.Win.CalcEngine


    Class CustomFormulaValueConverter
        Implements IFormulaValueConverter

        Private Function ConvertFromUltraCalcValue(ByVal formulaResult As UltraCalcValue, ByVal context As Object) As Object Implements IFormulaValueConverter.ConvertFromUltraCalcValue
            ' ConvertFromUltraCalcValue gets called by the UltraWinGrid whenever a
            ' formula is evaluated. You typically use this method to convert the formula
            ' result which may be in one type or format to the type and format of the
            ' column. NOTE: The calc engine uses invariant culture to parse and format
            ' values.

            System.Diagnostics.Debug.WriteLine("ConvertFromUltraCalcValue called:")

            ' Context parameter is the object that's the target of the formula
            ' evaluation. It's either an UltraGridCell or SummaryValue instance in the
            ' case of UltraWinGrid.
            System.Diagnostics.Debug.WriteLine("     TypeOf Context is " & context.GetType().Name)

            If formulaResult.IsError Then
                ' Handle situations where the result of the formula evaluation is error.
                System.Diagnostics.Debug.WriteLine("     Formula TypeOf result is error: " & formulaResult.ToString())

                ' Return the value you want to assigned to the cell or the summary.
                Return "#E"
            Else
                System.Diagnostics.Debug.WriteLine("     Formula evaluated to: " & formulaResult.ToString())

                ' Return the value you want to assigned to the cell or the summary. 
                ' Typically you return a value that's the same type as the column's data type.
                Return formulaResult.ToDecimal()
            End If
        End Function

        Private Function ConvertToUltraCalcValue(ByVal cellValue As Object, ByVal context As Object) As UltraCalcValue Implements IFormulaValueConverter.ConvertToUltraCalcValue
            ' ConvertToUltraCalcValue gets called by the UltraWinGrid whenever a cell or
            ' summary value is fed to a formula during evaluation. You typically use this
            ' method to convert the cell values which may be in one format (for example
            ' the US culture format) to the format that's appropriate for the formula
            ' evaluation. NOTE: The calc engine uses invariant culture to parse and
            ' format values.

            System.Diagnostics.Debug.WriteLine("ConvertToUltraCalcValue called:")

            ' Context parameter is the object associated with the cell value. It's either
            ' an UltraGridCell or SummaryValue instance in the case of UltraWinGrid
            ' objects however it could be UltraCalcSettings or NamedReference instances
            ' as well if the formula refers to a control outside of the grid or a named
            ' reference.
            System.Diagnostics.Debug.WriteLine("     TypeOf Context is " & context.GetType().Name)

            If Nothing Is cellValue Or DBNull.Value Is cellValue Then
                System.Diagnostics.Debug.WriteLine("     Value being TypeOf converted is (NULL)")
                Return New UltraCalcValue(cellValue)
            End If

            System.Diagnostics.Debug.WriteLine("     Value being TypeOf converted is " & cellValue.ToString())
            Dim val As Double = Double.Parse(cellValue.ToString())
            Return New UltraCalcValue(val)
        End Function

    End Class

    Private Sub UltraGrid1_InitializeLayout(ByVal sender As Object, ByVal e As Infragistics.Win.UltraWinGrid.InitializeLayoutEventArgs) Handles ultraGrid1.InitializeLayout
        Dim column As UltraGridColumn = Me.ultraGrid1.DisplayLayout.Bands(0).Columns(0)

        ' Add a summary that calculates the total of the column.
        Dim summary As SummarySettings = column.Band.Summaries.Add("sum( [" & column.Key & "] )")

        ' Set the FormulaValueConverter on both the column and summary.
        column.FormulaValueConverter = New CustomFormulaValueConverter()
        summary.FormulaValueConverter = New CustomFormulaValueConverter()
    End Sub
C#Copy Code
using Infragistics.Shared;
using Infragistics.Win;
using Infragistics.Win.UltraWinGrid;
using System.Diagnostics;
using Infragistics.Win.CalcEngine;


		private class CustomFormulaValueConverter : IFormulaValueConverter
		{
			public object ConvertFromUltraCalcValue( UltraCalcValue formulaResult, object context )
			{
				// ConvertFromUltraCalcValue gets called by the UltraWinGrid whenever a
				// formula is evaluated. You typically use this method to convert the formula
				// result which may be in one type or format to the type and format of the
				// column. NOTE: The calc engine uses invariant culture to parse and format
				// values.

				System.Diagnostics.Debug.WriteLine( "ConvertFromUltraCalcValue called:" );

				// Context parameter is the object that's the target of the formula
				// evaluation. It's either an UltraGridCell or SummaryValue instance in the
				// case of UltraWinGrid.
				System.Diagnostics.Debug.WriteLine( "     Context is " + context.GetType( ).Name );

				if ( formulaResult.IsError )
				{
					// Handle situations where the result of the formula evaluation is error.
					System.Diagnostics.Debug.WriteLine( "     Formula result is error: " + formulaResult.ToString( ) );

					// Return the value you want to assigned to the cell or the summary.
					return "#E";
				}
				else
				{
					System.Diagnostics.Debug.WriteLine( "     Formula evaluated to: " + formulaResult.ToString( ) );

					// Return the value you want to assigned to the cell or the summary. 
					// Typically you return a value that's the same type as the column's data type.
					return formulaResult.ToDecimal( );
				}
			}

			public UltraCalcValue ConvertToUltraCalcValue( object cellValue, object context )
			{
				// ConvertToUltraCalcValue gets called by the UltraWinGrid whenever a cell or
				// summary value is fed to a formula during evaluation. You typically use this
				// method to convert the cell values which may be in one format (for example
				// the US culture format) to the format that's appropriate for the formula
				// evaluation. NOTE: The calc engine uses invariant culture to parse and
				// format values.

				System.Diagnostics.Debug.WriteLine( "ConvertToUltraCalcValue called:" );

				// Context parameter is the object associated with the cell value. It's either
				// an UltraGridCell or SummaryValue instance in the case of UltraWinGrid
				// objects however it could be UltraCalcSettings or NamedReference instances
				// as well if the formula refers to a control outside of the grid or a named
				// reference.
				System.Diagnostics.Debug.WriteLine( "     Context is " + context.GetType( ).Name );

				if ( null == cellValue || DBNull.Value == cellValue )
				{
					System.Diagnostics.Debug.WriteLine( "     Value being converted is (NULL)" );
					return new UltraCalcValue( cellValue );
				}

				System.Diagnostics.Debug.WriteLine( "     Value being converted is " + cellValue.ToString( ) );
				double val = double.Parse( cellValue.ToString( ) );
				return new UltraCalcValue( val );
			}
		}

		private void ultraGrid1_InitializeLayout(object sender, Infragistics.Win.UltraWinGrid.InitializeLayoutEventArgs e)
		{
			UltraGridColumn column = this.ultraGrid1.DisplayLayout.Bands[0].Columns[0];

			// Add a summary that calculates the total of the column.
			SummarySettings summary = column.Band.Summaries.Add( "sum( [" + column.Key + "] )" );

			// Set the FormulaValueConverter on both the column and summary.
			column.FormulaValueConverter = new CustomFormulaValueConverter( );
			summary.FormulaValueConverter = new CustomFormulaValueConverter( );
		}

Remarks

FormulaValueConverter allows you to write custom logic for converting cell values to values that the formulas will use for calculations. It also allows you to write custom logic for converting results of formula calculations to cell values.

See Also

Reference

UltraGridColumn Class
UltraGridColumn Members
T:Infragistics.Win.CalcEngine.IFormulaValueConverter
Formula Property
CalcManager Property