Monday, November 24, 2014

Add Grid and AxToolbar in Dynamics AX EP details form

Here are the steps you can follow to add Grid with AxToolbar in any EP detail form:

  1. Create new WebActionMenu for Add and Delete (i.e. YourDetailFormAdd), Label=‘Add’, NormalImage= 11421)
  2. Create new Web menu YourDetailForm and drag both action menus to this web menu
  3. In your MasterDetail form in VS where you wants to place this detail grid, add new AxToolBar control and set WebMenuName= YourDetailForm, ID= YourDetailFormToolBar
  4. Add AxGridView control and set DS, AllowDelete,AllowEdit,AllowInset to True. And set Datasource and DataMember. Finally set DataKeyName=RecId.
  5. Open .cs file of your Form and declare two constants for menu items you created in AX:
  6. protected const string YOURDETAILForm_ADD= “yourdetailformadd";
  7. protected const string YOURDETAILFORM_DELETE= “yourdetailformdelete";
  8. Create new property to get your CurrentRow of your datasource.
  9. For Grid, we have to write event for (and called from Page_Load):
  10. RowUpdated
  11. RowEditing
  12. RowDeleting
  13. RowInserted
  14. RowCreated
  15. RowCancelingEdit
  16. For ToolBar, we have to write following event and called from OnInit
  17. ActionMenuItemClicking
  18. ActionMenuItemClicked
  19. SetMenuItemProperties
Here is the example of C# code behind:

using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.Dynamics.Framework.Portal.UI;
using Microsoft.Dynamics.Framework.Portal.UI.WebControls;
using Microsoft.Dynamics.AX.Framework.Services.Client;
using Microsoft.Dynamics.AX.Framework.Portal.Data;
using Proxy = Microsoft.Dynamics.Framework.BusinessConnector.Proxy;
using ApplicationProxy = Microsoft.Dynamics.Portal.Application.Proxy;
using Microsoft.Dynamics.Framework.BusinessConnector.Adapter;
using Microsoft.Dynamics.Framework.Portal;
using Microsoft.Dynamics.Portal.Application.Proxy;
using Microsoft.Dynamics.Framework.BusinessConnector.Session;
using Microsoft.Dynamics.Framework.Portal.UI.WebControls.WebParts;

public partial class YourForm: System.Web.UI.UserControl
{
    protected const string COSTTYPE_ADD = "hrminjuryincidentcosttypeadd";
    protected const string COSTTYPE_DELETE = "hrminjuryincidentcosttyperemove";
    
 
    protected void Page_Load(object sender, EventArgs e)
    {
        this.SetupMode();

        if (this.WebPart != null)
        {
            // Cost type
            AxCostGrid.RowUpdated += new GridViewUpdatedEventHandler(CostGrid_RowUpdated);
            AxCostGrid.RowCancelingEdit += new GridViewCancelEditEventHandler(CostGrid_RowCancelingEdit);
            AxCostGrid.RowEditing += new GridViewEditEventHandler(CostGrid_RowEditing);
            AxCostGrid.RowDeleting += new GridViewDeleteEventHandler(CostGrid_RowDeleting);

        }
        
    }

    protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);

        // Cost Type
        this.InjuryCostToolBar.ActionMenuItemClicking += new EventHandler(InjuryCostToolBar_ActionMenuItemClicking);
        this.InjuryCostToolBar.ActionMenuItemClicked += new EventHandler(InjuryCostToolBar_ActionMenuItemClicked);
        this.InjuryCostToolBar.SetMenuItemProperties += new EventHandler(InjuryCostToolBar_SetMenuItemProperties);
        this.AxCostGrid.RowInserted += new EventHandler(CostGrid_RowInserted);
        this.AxCostGrid.RowCreated += new GridViewRowEventHandler(CostGrid_RowCreated);
        this.AxCostGrid.RowEditing += new GridViewEditEventHandler(CostGrid_RowEditing);

    }

    ApplicationProxy.EPFormAction FormMode
    {
        get
        {
            return (ApplicationProxy.EPFormAction)Convert.ToInt16(
            this.Page.Request.QueryString.Get("mode")); // This mode is the param set in web menu item url, i.e. mode=1
        }
    }
    private ISession AxSession
    {
        get
        {
            AxBaseWebPart webpart = AxBaseWebPart.GetWebpart(this);
            return webpart == null ? null : webpart.Session;
        }
    }
    private void SetupMode()
    {
        //Proxy.Info objInfoLog = new Proxy.Info(this.AxSession.AxaptaAdapter);
        //objInfoLog.add(Proxy.Exception.Warning, Convert.ToString(this.FormMode));
        switch (this.FormMode)
        {
            case ApplicationProxy.EPFormAction.EditMode:
                
                this.HRMInjuryIncidentForm.DefaultMode = DetailsViewMode.Edit;
                this.HRMInjuryIncidentForm.AutoGenerateEditButton = true;
                this.HRMInjuryIncidentForm.AutoGenerateInsertButton = false;
                break;
            
            case ApplicationProxy.EPFormAction.CreateMode:
                this.HRMInjuryIncidentForm.DefaultMode = DetailsViewMode.Insert;
                this.HRMInjuryIncidentForm.AutoGenerateEditButton = false;
                this.HRMInjuryIncidentForm.AutoGenerateInsertButton = true;
                break;

            default:
                this.HRMInjuryIncidentForm.DefaultMode = DetailsViewMode.ReadOnly;
                this.HRMInjuryIncidentForm.AutoGenerateEditButton = false;
                this.HRMInjuryIncidentForm.AutoGenerateInsertButton = false;
                
                this.AxCostGrid.AllowEdit = false;
                this.AxCostGrid.AllowDelete = false;
                break;
        }
    }

    /// COST TYPE
    private DataSetViewRow CostInjuryCurrentRow
    {
        get
        {
            try
            {
                DataSetView dsv = this.AxHRMInjuryIncidentDS.GetDataSet().DataSetViews[this.AxCostGrid.DataMember];
                return (dsv == null) ? null : dsv.GetCurrent();
            }
            // CurrentRow on the dataset throws exception in empty data scenarios
            catch (System.Exception)
            {
                return null;
            }
        }
    }

    private AxBaseWebPart WebPart
    {
        get { return AxBaseWebPart.GetWebpart(this); }
    }

    void CostGrid_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        addClicked = false;
    }

          
    void CostGrid_RowEditing(object sender, GridViewEditEventArgs e)
    {
        addClicked = false;
    }
     
    void CostGrid_RowCreated(object sender, GridViewRowEventArgs e)
    {
        addClicked = false;
    }

    void CostGrid_RowInserted(object sender, GridViewInsertedEventArgs e)
    {
        addClicked = false;
    }
    
    void InjuryCostToolBar_SetMenuItemProperties(object sender, SetMenuItemPropertiesEventArgs e)
    {
        long record = 0;

        AxCostGrid.AllowEdit = this.FormMode != ApplicationProxy.EPFormAction.InfoMode;// true;
        
        if (this.CostInjuryCurrentRow != null)
            record = (long)this.CostInjuryCurrentRow.GetFieldValue("RecId");

        // Set the properties on the toolbar
        switch (e.MenuItem.MenuItemAOTName.ToLower())
        {
            case COSTTYPE_DELETE:
                e.MenuItem.Disabled = !(this.HRMInjuryIncidentDS.GetDataSet().DataSetViews[this.AxCostGrid.DataMember].Count > 0 ? true : false);
                break;

            case COSTTYPE_ADD:
                if (addClicked)
                {
                    e.MenuItem.Disabled = true;
                }
                else
                {
                    if (this.AxCostGrid.AllowEdit)
                    {
                        e.MenuItem.Disabled = false;
                    }
                    else
                    {
                        e.MenuItem.Disabled = true;
                    }
                }
                break;
        }
    }

    void InjuryCostToolBar_ActionMenuItemClicked(object sender, ActionMenuItemEventArgs e)
    {
        switch (e.MenuItem.MenuItemAOTName.ToLower())
        {
            // The add button 
            case COSTTYPE_ADD:
                try
                {
                    this.AxCostGrid.AllowInsert = true;

                    this.AxCostGrid.CreateRow();

                    addClicked = true;

                    // Disable the add button
                    e.MenuItem.Disabled = true;
                }
                catch (System.Exception exception)
                {
                    AxExceptionCategory exceptionCategory;
                    // Check if the exception was a fatal exception
                    if (!AxControlExceptionHandler.TryHandleException(this, exception, out exceptionCategory))
                    {
                        // Fatal exception, throw so as to be handled
                        throw;
                    }
                }
                break;

            // The delete button 
            case COSTTYPE_DELETE:
                try
                {
                    int selectedIndex = this.AxCostGrid.SelectedIndex;
                    if (selectedIndex != -1)
                    {
                        this.AxCostGrid.DeleteRow(selectedIndex);
                    }
                }
                catch (System.Exception ex)
                {
                    AxExceptionCategory exceptionCategory;
                    // This returns true if the exception can be handled here
                    if (!AxControlExceptionHandler.TryHandleException(this, ex, out exceptionCategory))
                    {
                        // The exception was fatal - in this case we re-throw.
                        throw;
                    }
                }
                break;
        }
    }

    void InjuryCostToolBar_ActionMenuItemClicking(object sender, ActionMenuItemClickingEventArgs e)
    {
        switch (e.MenuItem.MenuItemAOTName.ToLower())
        {
            case COSTTYPE_ADD:
            case COSTTYPE_DELETE:
                e.RunMenuItem = false;
                break;
        }
    }

    
    void CostGrid_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        try
        {
            // Only call CancelEdit when new row since we are manually adding it
            if (this.CostInjuryCurrentRow.IsNew)
                this.CostInjuryCurrentRow.CancelEdit();
        }
        catch (System.Exception exception)
        {
            AxExceptionCategory exceptionCategory;
            // Check if the exception was a fatal exception
            if (!AxControlExceptionHandler.TryHandleException(this, exception, out exceptionCategory))
            {
                // Fatal exception, throw so as to be handled
                throw;
            }
        }

        CancelRowEdit();
    }

    private void CancelRowEdit()
    {
        AxCostGrid.EditIndex = -1;
        AxCostGrid.AllowEdit = false;
    }


    void CostGrid_RowUpdated(object sender, GridViewUpdatedEventArgs e)
    {
        if (e.Exception == null)
        {
            DataSetViewRow dsvr = this.CostInjuryCurrentRow;

            if (dsvr != null)
            {
                bool rowSaved = true;
                try
                {
                    if (dsvr.IsNew)
                        dsvr.EndEdit();
                }
                catch (System.Exception ex)
                {
                    rowSaved = false;

                    AxExceptionCategory exceptionCategory;
                    // This returns true if the exception can be handled here
                    if (AxControlExceptionHandler.TryHandleException(this, ex, out exceptionCategory))
                    {
                        // If non fatal run error compensation logic
                        if (exceptionCategory == AxExceptionCategory.NonFatal)
                        {
                            // Keeps the current row editable i.e. reverts to the previous UI state
                            e.KeepInEditMode = true;
                        }
                    }
                    else
                    {
                        // The exception is system fatal - in this case we re-throw.                                    
                        throw;
                    }
                }

                if (rowSaved)
                {
                    CancelRowEdit();
                }
            }
        }
    }


    protected AxBoundField GetField(string name)
    {
        AxBoundField attendeeField = null;

        // Get the field which needs to have the customized lookup
        foreach (DataControlField field in this.AxCostGrid.DataControlFieldCollection)
        {
            AxBoundField boundField = field as AxBoundField;
            // If the field is not null and has the required data field
            if (boundField != null && String.Compare(boundField.DataField, name, true) == 0)
            {
                attendeeField = boundField;
                break;
            }
        }

        return attendeeField;
    }
}

Happy DAXing !!!!!

No comments: