Monday, April 26, 2010

Programmatically Inserting Data into a Grid

During many .NET based projects, I have utilized DataGrids in both Windows Form and Web based applications to hold temporary non-bound data used primarily for user verification purposes. More recently, I was faced with a similar requirement in a needed AX modification, but due to “limitations” of the AX Grid control, I was forced to re-think my approach.

The problem: there does not appear to be any way to programmatically add data to a grid; and if there actually is, I have yet to find it. So it seems, that grids must always be bound to a data source. The way around this so-called “limitation”, is to utilize a Temporary Table. A Temporary Table would be used because, unlike standard tables, it does not map to the a relational database, and will only contain data when an active record buffer is attached to it. Temporary record buffers are isolated to the user, thus if two different users are accessing the Temporary Table, they are actually pointed to different datasets. Therefore, if User (A) and User (B) are using the same form at the same time, there would not be a conflict in data integrity.

In my required modification, a form was needed to allow a user to choose from a list of customers, giving them the ability to add their selections to a grid. Once the user verified that the gird contained all of the customers they required, they could then process this list with a click of the mouse. For this particular tutorial, I will explain the basic method that I have discovered to accomplishing this task of “programmatically inserting data into a grid”.

Let’s say we want a Grid to hold a selected list of Customer Names & Numbers. What we would want to do, is create a Table in the AOT, and mark it as Temporary, containing two string fields; Name, CustNumber. We would then want to set the Data Source of our Grid to this Table, and then make a few X++ modifications

Do the following in the AOT:
1. Create a new Table, and name it TempCustList
2. Add two string fields: CustNumber, and Name
3. Right-Click the Table, and select Properties
4. Set the Temporary property to “Yes”
5. Create a new Form, and name it FrmCustList
6. Drag-and-Drop the TempCustList Table into the Form’s Data Sources
7. Expand down to the Form’s Design node.
8. Right-Click the Design node and select: New ControlGrid
9. Right-Click the Grid, and select Properties
10. Set the Name property to “GridCustList
11. Set the Data Source property to “TempCustList
12. Perform the following:

In the FrmCustList Form where we will be using the Temporary Table, we will want to declare the record buffer so that we can access the data in the temporary table anywhere in the form without losing it (as long as our Form remains open). For example, If you declare the Temporary Table within a mouse click method on a form’s button for example, and insert data into the Temporary Table, the data will be lost, as soon as the method completes.

public class FormRun extends ObjectRun
{
TempCustList temp;
}
13. On the Form, add a Button and override it’s click() method with the following code:

void clicked()
{

// Test data, replace with a query to pull actual customer info, based on selection…
temp.Name = "Joe Smith";
temp.CustNumber = "CUST-0001";

// Insert into the Temp table
temp.insert();

// Refresh the Data Source, which then refreshes the Grid…
TempCustList.setTmpData(temp);
TempCustList_ds.executeQuery();

super();
}
The above code, will obviously add the same line to the Grid every time it is clicked, however the scope of this tutorial was simply to show you how to add data to a grid “programmatically”. A simple modification to the code would allow you to utilize the example for your needs.

3 comments:

  1. This Code Works Fine.........
    Thanks :D

    ReplyDelete
  2. Hi
    I do have following method in the RDP class, but the data is not pushing into Temp tables.

    private void setARG_SOCustReqRelationTmp()
    {


    ARG_SOCustReqRelation arg_SOCustReqRelation;

    arg_SOCustReqRelationTmp.setTmp();
    arg_SOCustReqRelationTmp.recordLevelSecurity(true);

    ttsBegin;
    // Clear the data from the copy table
    //delete_from arg_SOCustReqRelationTmp;


    while select arg_SOCustReqRelation
    {
    arg_SOCustReqRelationTmp.SalesId = arg_SOCustReqRelation.SalesId;
    arg_SOCustReqRelationTmp.ARG_CustReq = arg_SOCustReqRelation.ARG_CustReq;

    info(strFmt("Inserted a row for ",arg_SOCustReqRelation.ARG_CustReq));


    arg_SOCustReqRelationTmp.insert();
    }

    ttsCommit;
    }

    what can I Do.

    I'd appreciate your time.
    M.M Rahman

    ReplyDelete