Add NOV Grid control in WinForms at runtime

Add NOV Grid control in WinForms at runtime



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

The following topic shows how to add a NOV Grid Widget (control) in WinForms 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.WinForm.dll - presentation host for Window Forms;
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.Grid;
using Nevron.Nov.Windows.Forms;
using System;
using System.Windows.Forms;
 
namespace WindowsFormsApplication1
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
 
            // Apply license for redistribution here. You can skip this code when evaluating NOV.
            NLicenseManager.Instance.SetLicense(new NLicense("LICENSE KEY"));
            // Install NOV
            NModule[] modules = new NModule[] {
                // TODO: Create modules here
                NGridModule.Instance
            };
            NNovApplicationInstaller.Install(modules);
            Application.Run(new Form1());
        }
    }
}

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

using Nevron.Nov.Data;
using Nevron.Nov.Grid;
using Nevron.Nov.Windows.Forms;
using System;
using System.Windows.Forms;
 
namespace WindowsFormsApplication2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            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);
            host.Dock = DockStyle.Fill;
            Controls.Add(host);
        }
 
        private void LoadData(NTableGridView tableGridView)
        {
            NTableGrid tableGrid = tableGridView.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 form with a Grid bound to the dummy data source.




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

Imports Nevron.Nov
Imports Nevron.Nov.Windows.Forms
Imports Nevron.Nov.Grid
Imports Nevron.Nov.Data
 
Public Class Form1
    Inherits Form
    Public Sub New()
        InitializeComponent()
        ' clear all controls from the form
        Controls.Clear()
 
        ' 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
        Dim host As New NNovWidgetHost(Of NTableGridView)(tableGridView)
        host.Dock = DockStyle.Fill
 
        ' add the host in the form controls
        Controls.Add(host)
    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: 247, Created On: 8/11/2016, Modified: 9/27/2016