Wednesday, February 18, 2009

Creating a Matrix form in Dynamics NAV

I have always just copied matrix forms and hacked them up to create a new one.  Today, after developing in NAV since it was called Avista in 1996, I created one from scratch.  Here are the steps (sorry there are no images, maybe later):

Create a New, blank form, set the SourceTable of the form to the record to be used as the rows in the matrix.  Add any columns you want on each row to the left of the matrix using the Field Menu.

Create 2 text values: MatrixValue, and MatrixHeader.

Put a Matrix control on the form, assign the MatrixSourceTable to be the record defining the columns of the matrix.

If the form is to be focused on another record (like a matrix of sizes and colors for a single item), you can create a function used to open the form, create the record as a global variable and get the source record in that function.

Add a text box to the right section of the matrix, set the sourceexpression to MatrixValue.
Add a text box to the header portion of the new column, set the sourceexpression to MatrixHeader

If the matrix rec (the record defining the number of columns in the matrix) is bound to the source rec, call 

On form open, set filter on the SourceTable variable, using the source record, changing to filter group 2 before, and then 0 afterward, then filter the MatrixSourceTable if needed as follows:

CurrForm.Matrix.MatrixRec.SETRANGE("Field",SourceTable."Field");

Select the entire Matrix control, and in the Matrix-OnAfterGetRecord() trigger:

MatrixHeader := CurrForm.Matrix.MatrixRec."Field";

Or any other value.  In my case, I appended the code with the description since the users needed both.

Select the right portion of the Matrix control and go to the C/AL code.

In the OnFormat section assign the text to whatever you want displayed in each grid box, using the Currform.Matrix.MatrixRec to determine the Column, and the SourceTable for the row, and the SourceRecord if needed.

That's it!