In this chapter, we learned how to create custom visual controls, call add-in methods from NAV application code, and raise and handle control events. Almost all recipes so far covered controls that accept scalar values in parameter methods. This is good for extensions displaying a single value, but if you need to show a table control, sending values for each table cell one by one is not the best way to fill the dataset. Recipes dedicated to JavaScript controls, explain one possible solution for this problem - sending data to the control as a JSON string. JSON format is native to a JavaScript environment. In .NET controls, wrapping data in a DataTable object is a more common approach.
This recipe shows how to create a custom DataGridView
control that shows a list of customers in a table. To populate the control with data, we will employ the DataTable .NET object.
NAVDataGridView
and choose to create a new solution.Class1.cs
and rename it to NAVDataGrid.cs
. Accept the request to rename code references to the Class1
class.Microsoft.Dynamics.Framework.UI.Extensibility.dll
and System.Windows.Forms
assemblies.using Microsoft.Dynamics.Framework.UI.Extensibility; using Microsoft.Dynamics.Framework.UI.Extensibility.WinForms; using System.Windows.Forms; using System.Data;
ControlAddInExport
attribute to the NavDataGrid
class:namespace NAVDataGridView { [ControlAddInExport("NAVDataGridView")] public class NAVDataGrid : WinFormsControlAddInBase { } }
WinForms
control, DataGridView
, which will be displayed in the page, is declared as a private property in NAVDataGridView
. Insert the declaration in the class:private DataGridView dataGridView;
dataGridView
property:[ApplicationVisible] public event ControlAddInEventHandler ControlAddIn;
CreateControl
method that overrides the abstract method of the base class:protected override Control CreateControl() { dataGridView = new DataGridView(); dataGridView.Dock = DockStyle.Fill; dataGridView.ReadOnly = true; ControlAddIn(0, string.Empty); return dataGridView; }
UpdateView
method will be called from NAV C/AL code to update contents of the data grid control. Declare the method and mark it [ApplicationVisible]
to expose it to NAV server:[ApplicationVisible] public void UpdateView(DataTable dataTable) { dataGridView.DataSource = dataTable; }
NAVDataGridView
.ContentArea
container and add one field to the container. Enter Customers
in the Name
property of the field.ControlAddIn
property of the field and select the add-in registered in Step 11.UpdateControl
function. Declare the following local variables in the function:
Name |
DataType |
SubType |
Customer |
Record |
Customer |
DataTable |
DotNet |
System.Data.DataTable |
DataColumn |
DotNet |
System.Data.DataColumn |
DataRow |
DotNet |
System.Data.DataRow |
DataTable := DataTable.DataTable; DataColumn := DataColumn.DataColumn; DataColumn.ColumnName := 'No.'; DataTable.Columns.Add(DataColumn); DataColumn := DataColumn.DataColumn; DataColumn.ColumnName := 'Name'; DataTable.Columns.Add(DataColumn); Customer.FINDSET; REPEAT DataRow := DataTable.NewRow; DataRow.Item(0,Customer."No."); DataRow.Item(1,Customer.Name); DataTable.Rows.Add(DataRow); UNTIL Customer.NEXT = 0; CurrPage.Customers.UpdateView(DataTable);
Customers - OnControlAddIn
and invoke the UpdateControl
function from it:UpdateControl;
DataGridView
control.In the first part of the recipe, a control add-in assembly is developed and compiled in Visual Studio. Declaration of the add-in is almost identical to developing a WinForms add-in described in Developing a control add-in, except for a ControlAddIn
event that must be raised when the control is created and ready to receive data. Note that the event has an [ApplicationVisible]
attribute.
Step 8 instantiates the DataGridView
control that will be displayed in the page by implementing the CreateControl
abstract method inherited from WinFormsControlAddInBase
. This is the standard method of creating an add-in instance, except for the invocation of the ControlAddIn
event prior to returning the instance reference to the calling code:
ControlAddIn(0, string.Empty);
At this point, an instance of the DataGridView
control is already created and ready to receive the data to be displayed. The ControlAddIn
event is handled by a NAV client application that prepares the dataset.
The event handler that receives control is Customers - OnControlAddIn
in Step 16. The handler simply calls the UpdateControl
function, where each customer record is presented as a DataRow
object. All rows are inserted into a table represented by the DataTable
object. Finally, the formatted table is passed as a parameter to the control's UpdateView
function.
The UpdateView
method in Step 9 demonstrates why DataTable
is as natural for .NET as JSON is for JavaScript. All we need to bind the data received from NAV, to the control, is assign the variable to the DataSource
property of the control:
dataGridView.DataSource = dataTable;
.NET framework will do the rest. It takes care of parsing the data table and assigning values to individual table cells.
18.189.182.96