Exchanging data with add-ins

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.

How to do it...

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.

  1. Start a new C# project in Visual Studio based on the Class Library template. In the New Project window, type the project name NAVDataGridView and choose to create a new solution.
  2. In the solution explorer window, right-click on the name of the file Class1.cs and rename it to NAVDataGrid.cs. Accept the request to rename code references to the Class1 class.
  3. Do not leave the solution explorer. Right-click on References and add references to the Microsoft.Dynamics.Framework.UI.Extensibility.dll and System.Windows.Forms assemblies.
  4. Replace the using directives auto-generated by the framework, with the following:
            using Microsoft.Dynamics.Framework.UI.Extensibility; 
            using Microsoft.Dynamics.Framework.UI.Extensibility.WinForms; 
            using System.Windows.Forms; 
            using System.Data; 
    
  5. Add the base class reference and the ControlAddInExport attribute to the NavDataGrid class:
            namespace NAVDataGridView 
            { 
              [ControlAddInExport("NAVDataGridView")] 
              public class NAVDataGrid : WinFormsControlAddInBase 
              { 
              } 
            } 
    
  6. The 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; 
    
  7. We will need to raise an event to notify the NAV server when the control is initialized. Insert the event declaration after the dataGridView property:
            [ApplicationVisible] 
            public event ControlAddInEventHandler ControlAddIn; 
    
  8. Type the 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; 
            } 
    
  9. The 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; 
            } 
    
  10. Sign the assembly (refer to the Signing the control add-in assembly recipe if you require detailed description of the signing procedure).
  11. Build the project, then run NAV client and register the control add-in, as described in Registering and embedding a control add-in. The name of the control add-in should be NAVDataGridView.
  12. Run NAV Development Environment and create a blank page. In the page designer, insert a ContentArea container and add one field to the container. Enter Customers in the Name property of the field.
  13. Select the ControlAddIn property of the field and select the add-in registered in Step 11.
  14. Open C/AL editor. Declare a local 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

  15. Type or copy the function code:
            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); 
    
  16. Locate the control add-in trigger Customers - OnControlAddIn and invoke the UpdateControl function from it:
            UpdateControl; 
    
  17. Now the page is ready. Compile and run the object to see the list of customers in the custom DataGridView control.

How it works...

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.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.189.182.96