Add NOV Grid control in MonoMac project at runtime

Add NOV Grid control in MonoMac project at runtime



To get started and host NOV in your Mac OS X (MonoMac) application, take a look at: Getting Started with NOV in MonoMac project

The following topic shows how to add a NOV Grid Widget (control) in MonoMac project 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.MonoMac.dll - presentation host for MonoMac;
MonoMac.dll - make sure to reference the MonoMac.dll that comes with the NOV installation and remove the default one, which is created by Xamarin Studio;
Nevron.Nov.Grid.dll - assembly for the NGridModule;

You can reference the dlls directly from the System/Applications/NevronOpenVision.MonoMac.app/Contents/Bin folder or copy those dlls to a folder which is more convenient for referencing.

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

using System;
using Nevron.Nov;
using Nevron.Nov.Mac;
using Nevron.Nov.Grid;
 
#if UNIFIEDAPI
using Foundation;
using AppKit;
#else
using MonoMac.Foundation;
using MonoMac.AppKit;
#endif
 
namespace MonoMacApplication1
{
    class MainClass
    {
        static void Main(string[] args)
        {
            NSApplication.Init();
 
            NLicenseManager.Instance.SetLicense(new NLicense("LICENSE KEY"));
 
            // install NOV
            NModule[] modules = new NModule[]
            {
                // TODO: Create modules here
                NGridModule.Instance
            };
            NNOVApplicationInstaller.Install(modules);
 
            NSApplication.Main(args);
        }
    }
}

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

using System;
 
using Nevron.Nov.Mac;
using Nevron.Nov.UI;
using Nevron.Nov;
using Nevron.Nov.Grid;
using Nevron.Nov.Data;
 
#if UNIFIEDAPI
using Foundation;
using AppKit;
#else
using MonoMac.Foundation;
using MonoMac.AppKit;
#endif
 
namespace MonoMacApplication1
{
    public partial class MainWindow : NSWindow
    {
        #region Constructors
 
        // Called when created from unmanaged code
        public MainWindow(IntPtr handle)
            : base(handle)
        {
            Initialize();
        }
        // Called when created directly from a XIB file
        [Export("initWithCoder:")]
        public MainWindow(NSCoder coder)
            : base(coder)
        {
            Initialize();
        }
        // Shared initialization code
        void Initialize()
        {
            NApplication.ApplyTheme(new NMacElCapitanTheme());
 
            // create a table grid view
            NTableGridView view = new NTableGridView();
 
            // Load the data to the grid
            LoadData(view);
 
            // create NOV host
            this.ContentView = new NNovWidgetHost(view);
        }
 
        private void LoadData(NTableGridView view)
        {
            NTableGrid grid = view.Grid;
            grid.DataSource = CreateDummyDataSource();
            grid.AlternatingRows = true;
            grid.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);
        }
 
        #endregion
    }
}

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



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: 251, Created On: 8/12/2016, Modified: 8/12/2016