Work with matrix in sap b1 form

Work with matrix sap b1 form:-

Work with matrix in sap b1 form. In this post, I describe to you, how to work with matrix sap b1 from and Making the class for creating the form and manipulating the matrix.

Note-Don’t forget to add reference of two important UI and DI APIs namely:

1-SAPbouiCOM

2-SAPbobsCOM

[AdSense-A]

Step-1:Making the class for creating the form and manipulating the matrix.

 

using System;
using System.Windows.Forms;
class WorkWithMatrix
{

    private SAPbouiCOM.Application SBO_Application;
    private SAPbouiCOM.Form oForm;

    private SAPbouiCOM.Matrix oMatrix;
    private SAPbouiCOM.Columns oColumns;
    private SAPbouiCOM.Column oColumn;

    private SAPbouiCOM.DBDataSource oDBDataSource;

    // declaring a User data source for the “Remarks” Column
    private SAPbouiCOM.UserDataSource oUserDataSource;

    public WorkWithMatrix()
    {

        //this function definition has been defined into another post you can go on that code after clicking on the hyperlink.

        SetConnection();

        // Create the form with all controls specially matrix
        CreateFormWithMatrix();

        // Adding Data Sources to the Form
        AddingDataSourceToForm();

        // Binding the Form’s items with the desired data source
        BindingDataToForm();

        // Loading data to matrix
        GettingDataFromDataSource();

        // Making the form visible
        oForm.Visible = true;

        // events handled by SBO_Application_ItemEvent
        SBO_Application.ItemEvent += new SAPbouiCOM._IApplicationEvents_ItemEventEventHandler(SBO_Application_ItemEvent);
    }

    private void CreateFormWithMatrix()
    {

        // We are declaring the following object type of variable manipulate the controls on the form
        SAPbouiCOM.Item oItem = null;
        SAPbouiCOM.Button oButton = null;
        SAPbouiCOM.StaticText oStaticText = null;
        SAPbouiCOM.EditText oEditText = null;

        // The following object is needed to create our form
        SAPbouiCOM.FormCreationParams creationPackage;
        //object creationPackage = null;
        object tmp = null;
        SAPbouiCOM.FormCreationParams oCreationParams = null;

        tmp = SBO_Application.CreateObject(SAPbouiCOM.BoCreatableObjectType.cot_FormCreationParams);

        creationPackage = ((SAPbouiCOM.FormCreationParams)(SBO_Application.CreateObject(SAPbouiCOM.BoCreatableObjectType.cot_FormCreationParams)));

        creationPackage.UniqueID = “FrmUID123”;
        creationPackage.FormType = “TypeFrm123”;

        // Adding this form to the SBO application
        oForm = SBO_Application.Forms.AddEx(((SAPbouiCOM.FormCreationParams)(creationPackage)));

        // Setting the form properties
        oForm.Title = “Matrix, Datasources and Linked Button”;
        oForm.Left = 336;
        oForm.ClientWidth = 520;
        oForm.Top = 44;
        oForm.ClientHeight = 200;

        // Adding Items to the form and setting their properties
        oItem = oForm.Items.Add( “1”, SAPbouiCOM.BoFormItemTypes.it_BUTTON);
        oItem.Left = 5;
        oItem.Width = 65;
        oItem.Top = 170;
        oItem.Height = 19;

        oButton = ((SAPbouiCOM.Button)(oItem.Specific));
        //Simillarly
        oButton.Caption = “Ok”;
        oItem = oForm.Items.Add( “2”, SAPbouiCOM.BoFormItemTypes.it_BUTTON);
        oItem.Left = 75;
        oItem.Width = 65;
        oItem.Top = 170;
        oItem.Height = 19;

        oButton = ((SAPbouiCOM.Button)(oItem.Specific));
        oButton.Caption = “Cancel”;

        //Simillarly
        oItem = oForm.Items.Add( “txtPhone”, SAPbouiCOM.BoFormItemTypes.it_EDIT);
        oItem.Left = 265;
        oItem.Width = 163;
        oItem.Top = 172;
        oItem.Height = 14;

        // Adding an Add Phone prefix column button
        oItem = oForm.Items.Add( “BtnPhone”, SAPbouiCOM.BoFormItemTypes.it_BUTTON);
        oItem.Left = 160;
        oItem.Width = 100;
        oItem.Top = 170;
        oItem.Height = 19;

        oButton = ((SAPbouiCOM.Button)(oItem.Specific));

        oButton.Caption = “Add Phone prefix”;

        // This function adds the matrix on the form.
        AddingMatrixToForm();

    }

    private void AddingMatrixToForm()
    {

        SAPbouiCOM.Item oItem = null;
        SAPbouiCOM.LinkedButton oLink = null;

        // Adding a Matrix item on the form.
        oItem = oForm.Items.Add( “Matrix1”, SAPbouiCOM.BoFormItemTypes.it_MATRIX);
        oItem.Left = 5;
        oItem.Width = 500;
        oItem.Top = 5;
        oItem.Height = 150;

        oMatrix = ((SAPbouiCOM.Matrix)(oItem.Specific));
        //creating the instance of the column.
        oColumns = oMatrix.Columns;

        // Adding Culomn items to the matrix and making it as EditTextbox.
        oColumn = oColumns.Add( “#”, SAPbouiCOM.BoFormItemTypes.it_EDIT );
oColumn.TitleObject.Caption = “#”;
oColumn.Width = 30;
        oColumn.Editable = false;

        // Adding column for BP Card Code and making it as linked button.
        oColumn = oColumns.Add( “DSCardCode”, SAPbouiCOM.BoFormItemTypes.it_LINKED_BUTTON);
        oColumn.TitleObject.Caption = “Card Code”;
        oColumn.Width = 40;
        oColumn.Editable = true;

        //Linking the column with business partner masterdata
        oLink = ((SAPbouiCOM.LinkedButton)(oColumn.ExtendedObject));
        oLink.LinkedObject = SAPbouiCOM.BoLinkedObject.lf_BusinessPartner;

        // Adding column for BP Card Name and making it as EditTextBox
        oColumn = oColumns.Add( “DSCardName”, SAPbouiCOM.BoFormItemTypes.it_EDIT);
        oColumn.TitleObject.Caption = “Name”;
        oColumn.Width = 40;
        oColumn.Editable = true;

        // Add a column for BP Card Phone and making it as EditTextBox
        oColumn = oColumns.Add( “DSPhone”, SAPbouiCOM.BoFormItemTypes.it_EDIT);
        oColumn.TitleObject.Caption = “Phone”;
        oColumn.Width = 40;
        oColumn.Editable = true;

        // Add a column for BP Card Phone and making it as EditTextBox
        oColumn = oColumns.Add( “DSPhoneInt”, SAPbouiCOM.BoFormItemTypes.it_EDIT);
        oColumn.TitleObject.Caption = “Int.Phone”;
        oColumn.Width = 40;
        oColumn.Editable = true;
    }

    public void AddingDataSourceToForm()
    {

        //Adding UserDataSource to the from.
        oUserDataSource = oForm.DataSources.UserDataSources.Add( “IntPhone”, SAPbouiCOM.BoDataType.dt_SHORT_TEXT, 20);

        // Adding DB data sources
        oDBDataSource = oForm.DataSources.DBDataSources.Add( “OCRD” );
    }

    public void BindingDataToForm()
    {

        //We can get the column by its uid.
        oColumn = oColumns.Item( “DSCardCode” );
        //Setting the datasource
        oColumn.DataBind.SetBound(true, “OCRD”, “CardCode” );
        //We can get the column by its uid.
        oColumn = oColumns.Item( “DSCardName” );
        //Setting the datasource
        oColumn.DataBind.SetBound(true, “OCRD”, “CardName” );
        //We can get the column by its uid.
        oColumn = oColumns.Item( “DSPhone” );
        //Setting the datasource
        oColumn.DataBind.SetBound(true, “OCRD”, “Phone1” );
        //We can get the column by its uid.
        oColumn = oColumns.Item( “DSPhoneInt” );
        //Setting the datasource
        oColumn.DataBind.SetBound(true, “”, “IntPhone” );

    }

    public void GettingDataFromDataSource()
    {

        oMatrix.Clear();
        oMatrix.AutoResizeColumns();

        // Querying the DB Data source
        //parameter in the Query function is optional and basically it takes the Condition on which it filters the data.
        oDBDataSource.Query(null);

        // setting the value of UserDataSource
        oUserDataSource.Value = “Phone with prefix”;
        //Loading data into the matrix.
        oMatrix.LoadFromDataSource();

    }

    private void SBO_Application_ItemEvent(string FormUID, ref SAPbouiCOM.ItemEvent pVal, out bool BubbleEvent)
    {
        BubbleEvent = true;
        if ((pVal.FormUID == “UidFrmMatrix” ) ) {

            if (((pVal.ItemUID == “BtnPhone” ) &(pVal.EventType == SAPbouiCOM.BoEventTypes.et_ITEM_PRESSED) & (pVal.Before_Action == false) ) ) {
                AddNewPrefix();
            }

            if (((pVal.EventType == SAPbouiCOM.BoEventTypes.et_FORM_UNLOAD) & (pVal.Before_Action == false)))
            {
                SBO_Application.MessageBox( “Form Unloaded, Addon will terminate”, 1, “Ok”, “”, “” );
                System.Windows.Forms.Application.Exit();
            }
        }
    }

    public void AddNewPrefix()
    {
        int i = 0;
        SAPbouiCOM.Column PhoneExtCol = null;
        string newPhone = null;
        SAPbouiCOM.Item oItem = null;
        SAPbouiCOM.EditText oEditTxt = null;

        //Getting EditTextBox.
        oItem = oForm.Items.Item( “txtPhone” );
        oEditTxt = ((SAPbouiCOM.EditText)(oItem.Specific));

        // flushing the data from matrix to the table.
        oMatrix.FlushToDataSource();

        //Getting the DBdataSource.
        oDBDataSource = oForm.DataSources.DBDataSources.Item( “OCRD” );

        // Iterating all the records.
        for (i = 0; i <= oDBDataSource.Size – 1; i++ ) {
            newPhone = oDBDataSource.GetValue( “phone1″, i);
            newPhone = newPhone.Trim(char.Parse( ” ” ));
            //Setting it as new value.
            oDBDataSource.SetValue( “phone1”, i, oEditTxt.String + newPhone);
        }
        // Load data into the matrix from the DBDataSource.
        oMatrix.LoadFromDataSource();
    }
}

//Step-2: Creating the class for starting the project.

class Start
{

    public static void Main()
    {

        WorkWithMatrix obj = null;

        obj = new WorkWithMatrix();

    }

}

 

 

 


Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

error: Content is protected !!