Sorting with Formulas Example for WPF

Nevron NOV Grid for WPF ships with hundreds of examples with full source code, as part of the Nevron Open Vision Suite. All WPF Grid examples are also available as a stand-alone Visual Studio solution in C# and VB.NET, which you can download from the links below.

Sorting with Formulas Example for WPF Description

Demonstrates expression sorting rules in combination with calculated columns.

In this example we have created an NCustomCalculatedColumn that displays the Price * Quantity calculation (e.g. Total).

We have also created a sorting rule that sorts by the Total calculated column in a different way - via a Formula Expression.


Formula Sorting Example

C# Sorting with Formulas Example Code for Nevron Grid for WPF
Language: C#

using Nevron.Nov;
using Nevron.Nov.Dom;
using Nevron.Nov.Graphics;
using Nevron.Nov.Grid;
using Nevron.Nov.Layout;
using Nevron.Nov.UI;
using Nevron.Nov.Windows.Forms;
using System;
using System.Windows.Forms;

namespace NovWinFormsProject
{
	public partial class Form1 : Form
	{
		#region Constructors

		public Form1()
		{
			InitializeComponent();

			// Set form title
			Text = "NOV WinForms - NFormulaSortingExample";

			// Create the NOV example and its controls
			NWidget exampleContent = CreateExampleContent();
			NWidget exampleControls = CreateExampleControls();
			if (exampleControls != null)
			{
				exampleControls.PreferredWidth = 300;
			}

			// Place the example and its controls in a pair box
			NPairBox pairBox = new NPairBox(exampleContent, exampleControls);
			pairBox.FillMode = ENStackFillMode.First;
			pairBox.FitMode = ENStackFitMode.First;
			pairBox.Margins = new NMargins(NDesign.HorizontalSpacing, NDesign.VerticalSpacing);

			// Host the example in the form
			NNovWidgetHost host = new NNovWidgetHost(pairBox);
			host.Dock = DockStyle.Fill;
			Controls.Add(host);
		}

		#endregion

        #region Example

        public NWidget CreateExampleContent()
        {
            // create a view and get its grid
            NTableGridView view = new NTableGridView();
            NTableGrid grid = view.Grid;

            // bind the grid to the data source
            grid.DataSource = NDummyDataSource.CreatePersonsOrdersDataSource();

            // add a custom calculated column of type Double that displays the Total (e.g. Price * Quantity)
            NCustomCalculatedColumn totalColumn = new NCustomCalculatedColumn();
            totalColumn.Title = "Total";
            totalColumn.GetRowValueDelegate += delegate(NCustomCalculatedColumnGetRowValueArgs arg)
            {
                // calculate a RowValue for the RowIndex
                double price = Convert.ToDouble(arg.DataSource.GetValue(arg.RowIndex, "Price"));
                double quantity = Convert.ToDouble(arg.DataSource.GetValue(arg.RowIndex, "Quantity"));
                return (double)(price * quantity);
            };
            totalColumn.Format.BackgroundFill = new NColorFill(NColor.SeaShell);
            grid.Columns.Add(totalColumn);

            // create the sales sorting rule.
            // also subcribe for the create sorting rule event to recreate the rule when users 
            grid.SortingRules.Add(CreateTotalSortingRule(grid));
            totalColumn.CreateSortingRuleDelegate = delegate(NColumn theColumn)
            {
                return CreateTotalSortingRule(grid);
            };

            // alter some view preferences
            grid.AllowSortColumns = true;
            grid.AlternatingRows = true;
            grid.RowHeaders.Visible = true;

            return view;
        }
        public NWidget CreateExampleControls()
        {
            return null;
        }
        public string GetExampleDescription()
        {
            return @"

Demonstrates expression sorting rules in combination with calculated columns.

In this example we have created an NCustomCalculatedColumn that displays the Price * Quantity calculation (e.g. Total).

We have also created a sorting rule that sorts by the Total calculated column in a different way - via a Formula Expression.

"; } /// /// Gets the relevant documentation links for the example /// /// public (string Name, string Url)[] GetDocumentationUrls() { return new[] { ("Grid Sorting", "https://www.nevron.com/docs/openvision/grid-sorting.html"), ("Formula Basics", "https://www.nevron.com/docs/openvision/formula-basics.html"), ("Functions", "https://www.nevron.com/docs/openvision/formula-functions.html"), ("Grid Row Values", "https://www.nevron.com/docs/openvision/grid-row-values.html"), }; } #endregion #region Implementation NSortingRule CreateTotalSortingRule(NTableGrid grid) { NColumn salesColumn = grid.Columns.GetColumnByFieldName("Sales"); NColumn priceColumn = grid.Columns.GetColumnByFieldName("Price"); // create a sorting rule that sorts by the Total value string quantityFieldName = grid.CreateFormulaFieldName("Quantity"); string priceFieldName = grid.CreateFormulaFieldName("Price"); NSortingRule sortingRule = NSortingRule.FromFormula(null, priceFieldName + "*" + quantityFieldName, ENSortingDirection.Ascending); sortingRule.Column = salesColumn; return sortingRule; } #endregion #region Event Handlers #endregion #region Fields #endregion #region Static Methods #endregion #region Constants #endregion #region Nested Types #endregion } }

Download a fully functional C# (.cs) Sorting with Formulas Example for WPF Solution

VB.NET Sorting with Formulas Example Code for Nevron Grid for WPF
Language: VB.NET

Imports Nevron.Nov
Imports Nevron.Nov.Dom
Imports Nevron.Nov.Graphics
Imports Nevron.Nov.Grid
Imports Nevron.Nov.Layout
Imports Nevron.Nov.UI
Imports Nevron.Nov.Windows.Wpf
Imports System

Class MainWindow

#Region "Constructors"

	Public Sub New()
		' This call is required by the designer.
		InitializeComponent()

		' Add any initialization after the InitializeComponent() call.

		' Set form title
		Title = "NOV WPF - NFormulaSortingExample"

		' Create the NOV example and its controls
		Dim exampleContent As NWidget = CreateExampleContent()
		Dim exampleControls As NWidget = CreateExampleControls()
		If Not exampleControls Is Nothing Then
			exampleControls.PreferredWidth = 300
		End If

		' Place the example and its controls in a pair box
		Dim pairBox As NPairBox = New NPairBox(exampleContent, exampleControls)
		pairBox.FillMode = ENStackFillMode.First
		pairBox.FitMode = ENStackFitMode.First
		pairBox.Margins = New NMargins(NDesign.HorizontalSpacing, NDesign.VerticalSpacing)

		' Host the example in the form
		Dim host As NNovWidgetHost(Of NWidget) = New NNovWidgetHost(Of NWidget)(pairBox)
		Content = host
	End Sub


#End Region

#Region "Example"

        Public Function CreateExampleContent() As NWidget
            ' create a view and get its grid
            Dim view As NTableGridView = New NTableGridView()
            Dim grid = view.Grid

            ' bind the grid to the data source
            grid.DataSource = NDummyDataSource.CreatePersonsOrdersDataSource()

            ' add a custom calculated column of type Double that displays the Total (e.g. Price * Quantity)
            Dim totalColumn As NCustomCalculatedColumn(Of Double) = New NCustomCalculatedColumn(Of Double)()
            totalColumn.Title = "Total"
            totalColumn.GetRowValueDelegate += Function(arg As NCustomCalculatedColumnGetRowValueArgs(Of Double))
                                                   ' calculate a RowValue for the RowIndex
                                                   Dim price = Convert.ToDouble(arg.DataSource.GetValue(arg.RowIndex, "Price"))
                                                   Dim quantity = Convert.ToDouble(arg.DataSource.GetValue(arg.RowIndex, "Quantity"))
                                                   Return price * quantity
                                               End Function
            totalColumn.Format.BackgroundFill = New NColorFill(NColor.SeaShell)
            grid.Columns.Add(totalColumn)

            ' create the sales sorting rule.
            ' also subcribe for the create sorting rule event to recreate the rule when users 
            grid.SortingRules.Add(CreateTotalSortingRule(grid))
            totalColumn.CreateSortingRuleDelegate = Function(theColumn As NColumn) CreateTotalSortingRule(grid)

            ' alter some view preferences
            grid.AllowSortColumns = True
            grid.AlternatingRows = True
            grid.RowHeaders.Visible = True

            Return view
        End Function
        Public Function CreateExampleControls() As NWidget
            Return Nothing
        End Function
        Public Function GetExampleDescription() As String
            Return "

Demonstrates expression sorting rules in combination with calculated columns.

In this example we have created an NCustomCalculatedColumn that displays the Price * Quantity calculation (e.g. Total).

We have also created a sorting rule that sorts by the Total calculated column in a different way - via a Formula Expression.

" End Function ''' ''' Gets the relevant documentation links for the example ''' ''' Public Function GetDocumentationUrls() As (String, String)() Return {("Grid Sorting", "https://www.nevron.com/docs/openvision/grid-sorting.html"), ("Formula Basics", "https://www.nevron.com/docs/openvision/formula-basics.html"), ("Functions", "https://www.nevron.com/docs/openvision/formula-functions.html"), ("Grid Row Values", "https://www.nevron.com/docs/openvision/grid-row-values.html")} End Function #End Region #Region "Implementation" Private Function CreateTotalSortingRule(grid As NTableGrid) As NSortingRule Dim salesColumn As NColumn = grid.Columns.GetColumnByFieldName("Sales") Dim priceColumn As NColumn = grid.Columns.GetColumnByFieldName("Price") ' create a sorting rule that sorts by the Total value Dim quantityFieldName = grid.CreateFormulaFieldName("Quantity") Dim priceFieldName = grid.CreateFormulaFieldName("Price") Dim sortingRule = NSortingRule.FromFormula(Nothing, priceFieldName & "*" & quantityFieldName, ENSortingDirection.Ascending) sortingRule.Column = salesColumn Return sortingRule End Function #End Region #Region "Event Handlers" #End Region #Region "Fields" #End Region #Region "Static Methods" #End Region #Region "Constants" #End Region #Region "Nested Types" #End Region End Class

Download a fully functional VB.NET (.vb) Sorting with Formulas Example for WPF Solution