Add NOV Grid control in WPF at runtime



To get started and host NOV in your WPF application, take a look at: Getting Started with NOV in WPF using only code

The following topic shows how to add a NOV Grid Widget (control) in WPF at runtime. We are going to create a Data Table (NMemoryDataTable) with dummy data source and bind the Grid to the this data source.

1. Reference the NOV Assemblies
Ensure that your application references the following NOV dlls:
Nevron.Nov.Presentation.dll - core NOV portable assembly;
Nevron.Nov.Host.WinBase.dll - base assembly for Windows presentation hosts (WinForm and WPF);
Nevron.Nov.Host.Wpf.dll - presentation host for WPF;
Nevron.Nov.Grid.dll - assembly for the NGridModule;

These assemblies are located in NOV installation folder: C:\Program Files (x86)\Nevron Software\Nevron Open Vision VERSION\Bin\Win

2. When initializing the NOV Application, make sure to create the Grid module
Open the Program.cs file and use the following code:

using Nevron.Nov;
using Nevron.Nov.Windows;
using Nevron.Nov.Grid;
using System;
 
namespace WpfApplication1
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            // create the application
            App app = new App();
 
            // Apply license for redistribution here. You can skip this code when evaluating NOV.
            NLicenseManager.Instance.SetLicense(new NLicense("LICENSE KEY"));
             
            NModule[] modules = new NModule[] {
                  // TODO: Create modules here
                  NGridModule.Instance
            };
            // install Nevron Open Vision for WPF
            NNovApplicationInstaller.Install(modules);
            // run the application main window
            app.Run(new MainWindow());
        }
    }
}

3. Create the Grid (the example uses NTableGrid), data source and bind the Gird to the data source
Open the MainWindow.xaml.cs file and use the following code to create the Grid:

using Nevron.Nov.Data;
using Nevron.Nov.Grid;
using Nevron.Nov.Windows;
using System;
using System.Windows;
 
namespace WpfApplication1
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
 
            // Set window size
            this.Width = 620;
            this.Height = 300;
 
            // create a table grid view
            NTableGridView tableGridView = new NTableGridView();
             
            // Load the data to the grid
            LoadData(tableGridView);
 
            // create NOV host
            NNovWidgetHost<NTableGridView> host = new NNovWidgetHost<NTableGridView>(tableGridView);
            Content = host;
        }
 
        private void LoadData(NTableGridView gridView)
        {
            NTableGrid tableGrid = gridView.Grid;
            tableGrid.DataSource = CreateDummyDataSource();
            tableGrid.AlternatingRows = true;
            tableGrid.AllowEdit = true;
        }
 
        /// <summary>
        /// Creates the data source to bind the grid to.
        /// </summary>
        public static NDataSource CreateDummyDataSource()
        {
            NMemoryDataTable dataTable = new NMemoryDataTable(new NFieldInfo[]{
                new NFieldInfo("EmployeeID", typeof(Int32)),
                new NFieldInfo("Name", typeof(String)),
                new NFieldInfo("City", typeof(String)),
                new NFieldInfo("Birth Date", typeof(DateTime)),
                new NFieldInfo("Married", typeof(Boolean))
            });
 
            dataTable.AddRow(1, "Nancy Davolio", "Seattle", new DateTime(1968, 12, 8), true);
            dataTable.AddRow(2, "Andrew Fuller", "Tacoma", new DateTime(1952, 2, 19), true);
            dataTable.AddRow(3, "Janet Leverling", "Kirkland", new DateTime(1963, 8, 30), false);
            dataTable.AddRow(4, "Margaret Peacock", "Redmond", new DateTime(1958, 9, 19), false);
            dataTable.AddRow(5, "Steven Buchanan", "London", new DateTime(1955, 3, 4), true);
            dataTable.AddRow(6, "Michael Suyama", "London", new DateTime(1963, 7, 2), true);
            dataTable.AddRow(7, "Robert King", "London", new DateTime(1960, 5, 29), false);
            dataTable.AddRow(8, "Laura Callahan", "Seattle", new DateTime(1958, 1, 9), true);
            dataTable.AddRow(9, "Anne Dodsworth", "London", new DateTime(1969, 7, 2), false);
 
            return new NDataSource(dataTable);
        }
    }
}

Run the application - it should display a simple window with a Grid bound to the dummy data source.




Using Visual Basic .NET (VB.NET)
When you create a Visual Basic WPF Application, reference the NOV Assemblies and use the following code in your MainWindow.xaml.vb:

Imports Nevron.Nov
Imports Nevron.Nov.Grid
Imports Nevron.Nov.Data
Imports Nevron.Nov.Windows
 
Partial Public Class MainWindow
    Inherits Window
    Public Sub New()
        InitializeComponent()
        ' clear all controls from the form
 
        ' TODO: Apply license for redistribution here. You can skip this code when evaluating NOV.
        NLicenseManager.Instance.SetLicense(New NLicense("LICENSE KEY"))
 
        ' Install Nevron Open Vision modules
        Dim modules() As NModule = {NGridModule.Instance}
        NNovApplicationInstaller.Install(modules)
 
        ' Set window size
        Me.Width = 620
        Me.Height = 300
 
        ' create a table grid view
        Dim tableGridView As New NTableGridView()
 
        ' Load the data to the grid
        LoadData(tableGridView)
 
        ' create the host widget
        Content = New NNovWidgetHost(Of NTableGridView)(tableGridView)
    End Sub
 
    Private Sub LoadData(tableGridView As NTableGridView)
        Dim tableGrid As NTableGrid = tableGridView.Grid
        tableGrid.DataSource = CreateDummyDataSource()
        tableGrid.AlternatingRows = True
        tableGrid.AllowEdit = True
    End Sub
 
    ''' <summary>
    ''' Creates the data source to bind the grid to.
    ''' </summary>
    Public Shared Function CreateDummyDataSource() As NDataSource
        Dim dataTable As New NMemoryDataTable(New NFieldInfo() {New NFieldInfo("EmployeeID", GetType(Int32)), New NFieldInfo("Name", GetType([String])), New NFieldInfo("City", GetType([String])), New NFieldInfo("Birth Date", GetType(DateTime)), New NFieldInfo("Married", GetType([Boolean]))})
 
        dataTable.AddRow(1, "Nancy Davolio", "Seattle", New DateTime(1968, 12, 8), True)
        dataTable.AddRow(2, "Andrew Fuller", "Tacoma", New DateTime(1952, 2, 19), True)
        dataTable.AddRow(3, "Janet Leverling", "Kirkland", New DateTime(1963, 8, 30), False)
        dataTable.AddRow(4, "Margaret Peacock", "Redmond", New DateTime(1958, 9, 19), False)
        dataTable.AddRow(5, "Steven Buchanan", "London", New DateTime(1955, 3, 4), True)
        dataTable.AddRow(6, "Michael Suyama", "London", New DateTime(1963, 7, 2), True)
        dataTable.AddRow(7, "Robert King", "London", New DateTime(1960, 5, 29), False)
        dataTable.AddRow(8, "Laura Callahan", "Seattle", New DateTime(1958, 1, 9), True)
        dataTable.AddRow(9, "Anne Dodsworth", "London", New DateTime(1969, 7, 2), False)
 
        Return New NDataSource(dataTable)
    End Function
End Class


For more information about the NOV Grid component and Data Binding, take a look at the Help Documentation:
Grid > Grid Overview
Grid > Data Binging and Column Generation

 
Download Free Trial  Help Documentation
Send Feedback

Article ID: 248, Created On: 8/11/2016, Modified: 9/27/2016