Custom Grouping 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.

Custom Grouping Example for WPF Description

Demonstrates custom grouping, custom group header content and custom group row cells.

In this example we are grouping fictional emails by the Received field. However, since the condition for grouping is complex, we are using the NCustomRowValue to provide a custom row grouping condition.

The example also demonstrates how to create custom GroupRow cells. Since the email group values are instances of the ENMailGroup enumeration, the CreateGroupRowCellsDelegate is handled to provide a string representation for them.

The example also demonstrates how to override the CreateGroupingHeaderContentDelegate to provide a custom grouping rule header content. In example we have created, Received label serves as header content for the custom grouping rule.


Custom Grouping Example

C# Custom Grouping Example Code for Nevron Grid for WPF
Language: C#

using Nevron.Nov;
using Nevron.Nov.Data;
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 - NCustomGroupingExample";

			// 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()
        {
            // store current date time
            m_Now = DateTime.Now;

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

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

            // create a grouping rule with a custom row value.
            // NOTE: The RowValue associated with each grouping rule, returns an object for each row of the data source.
            // the rows in the data source are grouped according to that object.
            // The NCustomRowValue provides a delegate that help you return a custom object object for each data source row.
            // In our example the NCustomRowValue returns a member of the ENMailGroup enumeraiton for each record, depending on its Received row value.
            NCustomRowValue customRowValue = new NCustomRowValue();
            customRowValue.Description = "Received";
            customRowValue.GetRowValueDelegate = GetRowValueDelegate;

            // NOTE: The NGroupingRule provides the following events:
            // CreateGroupRowCells - raised when the grid needs to create the cells of the group row.
            // CreateGroupingHeaderContent - raised when the grid needs to create a grouping header content for the grouping in the groupings panel.
            NGroupingRule groupingRule = new NGroupingRule();
            groupingRule.RowValue = customRowValue;
            groupingRule.CreateGroupRowCellsDelegate = delegate(NGroupingRuleCreateGroupRowCellsArgs arg)
            {
                int groupValue = Convert.ToInt32(arg.GroupRow.GroupValue);
                string text = NStringHelpers.InsertSpacesBeforeUppersAndDigits(((ENMailGroup)groupValue).ToString());
                return new NGroupRowCell[] { new NGroupRowCell(text) };
            };

            groupingRule.CreateGroupingHeaderContentDelegate = delegate(NGroupingRule theGroupingRule)
            {
                return new NLabel("Received");
            };

            grid.GroupingRules.Add(groupingRule);

            return view;
        }


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

Demonstrates custom grouping, custom group header content and custom group row cells.

In this example we are grouping fictional emails by the Received field. However, since the condition for grouping is complex, we are using the NCustomRowValue to provide a custom row grouping condition.

The example also demonstrates how to create custom GroupRow cells. Since the email group values are instances of the ENMailGroup enumeration, the CreateGroupRowCellsDelegate is handled to provide a string representation for them.

The example also demonstrates how to the CreateGroupingHeaderContentDelegate to provide a custom grouping rule header content. In example we have created, Received label serves as header content for the custom grouping rule.

"; } /// /// Gets the relevant documentation links for the example /// /// public (string Name, string Url)[] GetDocumentationUrls() { return new[] { ("Grid Grouping", "https://www.nevron.com/docs/openvision/grid-grouping.html"), ("Grid Data Processing Overview", "https://www.nevron.com/docs/openvision/grid-data-processing-overview.html"), }; } #endregion #region Implementation #endregion #region Event Handlers #endregion #region Fields DateTime m_Now; #endregion #region Static Methods #endregion #region Constants #endregion #region Nested Types #endregion } }

Download a fully functional C# (.cs) Custom Grouping Example for WPF Solution

VB.NET Custom Grouping Example Code for Nevron Grid for WPF
Language: VB.NET

Imports Nevron.Nov
Imports Nevron.Nov.Data
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 - NCustomGroupingExample"

		' 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
            ' store current date time
            m_Now = Date.Now

            ' 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 = CreateMailDataSource()

            ' create a grouping rule with a custom row value.
            ' NOTE: The RowValue associated with each grouping rule, returns an object for each row of the data source.
            ' the rows in the data source are grouped according to that object.
            ' The NCustomRowValue provides a delegate that help you return a custom object object for each data source row.
            ' In our example the NCustomRowValue returns a member of the ENMailGroup enumeraiton for each record, depending on its Received row value.
            Dim customRowValue As NCustomRowValue(Of ENMailGroup) = New NCustomRowValue(Of ENMailGroup)()
            customRowValue.Description = "Received"
            customRowValue.GetRowValueDelegate = AddressOf GetRowValueDelegate

            ' NOTE: The NGroupingRule provides the following events:
            ' CreateGroupRowCells - raised when the grid needs to create the cells of the group row.
            ' CreateGroupingHeaderContent - raised when the grid needs to create a grouping header content for the grouping in the groupings panel.
            Dim groupingRule As NGroupingRule = New NGroupingRule()
            groupingRule.RowValue = customRowValue
            groupingRule.CreateGroupRowCellsDelegate = Function(arg As NGroupingRuleCreateGroupRowCellsArgs)
                                                           Dim groupValue = Convert.ToInt32(arg.GroupRow.GroupValue)
                                                           Dim text As String = NStringHelpers.InsertSpacesBeforeUppersAndDigits(CType(groupValue, ENMailGroup).ToString())
                                                           Return New NGroupRowCell() {New NGroupRowCell(text)}
                                                       End Function

            groupingRule.CreateGroupingHeaderContentDelegate = Function(theGroupingRule As NGroupingRule) New NLabel("Received")

            grid.GroupingRules.Add(groupingRule)

            Return view
        End Function


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

Demonstrates custom grouping, custom group header content and custom group row cells.

In this example we are grouping fictional emails by the Received field. However, since the condition for grouping is complex, we are using the NCustomRowValue to provide a custom row grouping condition.

The example also demonstrates how to create custom GroupRow cells. Since the email group values are instances of the ENMailGroup enumeration, the CreateGroupRowCellsDelegate is handled to provide a string representation for them.

The example also demonstrates how to override the CreateGroupingHeaderContentDelegate to provide a custom grouping rule header content. In example we have created, Received label serves as header content for the custom grouping rule.

" End Function ''' ''' Gets the relevant documentation links for the example ''' ''' Public Function GetDocumentationUrls() As (String, String)() Return {("Grid Grouping", "https://www.nevron.com/docs/openvision/grid-grouping.html"), ("Grid Data Processing Overview", "https://www.nevron.com/docs/openvision/grid-data-processing-overview.html")} End Function #End Region #Region "Implementation" #End Region #Region "Event Handlers" #End Region #Region "Fields" Private m_Now As Date #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) Custom Grouping Example for WPF Solution