Converting a MatrixTable to a DataTable, and vice versa

MatrixTable is a type (class) for in-memory representation of data tables just like DataTable of the .net framework ( MatrixTable has but a much simplified data structure compared to DataTable with primary focus on mathematical (or analytical) table functions rather than typical sql-based data operations like inserts and updates.

DataTable to or from MatrixTable

With the following methods of the MatrixTable class, a DataTable can easily be converted into a MatrixTable, and vice versa:

// From DataTable to MatrixTable
public static MatrixTable Import_from_DataTable(DataTable dtable, MetaData md,
   string TextReplaceNull = "NULL", int NumReplaceNull = 0, double KeyFigReplaceNull = 0)
// From MatrixTable to DataTable
public static DataTable Export_To_DataTable(MatrixTable tbl)

You can also find open-source DataTable extensions (c#/.net, license: GPL) that include methods for these conversions at CodePlex.

Converting DataTable to MatrixTable

In order to convert a DataTable object into a MatrixTable all the fields (columns) of DataTable must be predefined in MetaData with proper field types.

See the c# code example below:

using FinaquantProtos;
using System.Data;
// First, create a DataTable object
// For a helpful reference see:
DataTable DTbl = new DataTable();
DTbl.Columns.Add("product", typeof(string));
DTbl.Columns.Add("model", typeof(int));
DTbl.Columns.Add("date", typeof(DateTime));
DTbl.Columns.Add("sales", typeof(double));
// Add some rows to DataTable
DTbl.Rows.Add("Computer", 2010, DateTime.Now, 25000.0);
DTbl.Rows.Add("Telephone", 2008, new DateTime(2010, 8, 30), 8000.0);
DTbl.Rows.Add("Camera", 2012, new DateTime(2012, 9, 1), 34000.0);
// define all fields of DataTable in meta data
MetaData md = MetaData.CreateEmptyMetaData();
MetaData.AddNewField(md, "product", FieldType.TextAttribute);
MetaData.AddNewField(md, "model", FieldType.IntegerAttribute);
MetaData.AddNewField(md, "date", FieldType.DateAttribute);
MetaData.AddNewField(md, "sales", FieldType.KeyFigure);
// convert DataTable to MatrixTable
MatrixTable MTbl = MatrixTable.Import_from_DataTable(DTbl, md);
// view MatrixTable
MatrixTable.View_MatrixTable(MTbl, "MatrixTable converted from DataTable");

DataTable from MatrixTable

Converting MatrixTable to DataTable

Quite straightforward, see the code example below:

// make some changes in MatrixTable
MTbl.SetFieldValue(FieldName: "product", RowInd: 0, FieldValue: "Laptop");
MTbl.SetFieldValue(FieldName: "model", RowInd: 1, FieldValue: 1999);
// convert MatrixTable to DataTable
DTbl = MatrixTable.Export_To_DataTable(MTbl);
// view DataTable
DTbl.FQ_ViewTable("DataTable converted from MatrixTable");

DataTable from MatrixTable

Digiprove sealCopyright secured by Digiprove © 2013 Tunc Ali Kütükcüoglu
This entry was posted in Calculation engine and tagged , , . Bookmark the permalink.

1 Response to Converting a MatrixTable to a DataTable, and vice versa

  1. stevemannar says:

    You van create dataset manually … C# Dataset dynamic creation.


Leave a Reply