DynamicTable to DataTable

The DynamicTable object is a dynamic and expandable data class to store a table. You will find an introduction to this data class in a previous article. In the actual version of the DynamicTable I have added the functionality to export the data as DataTable and to import data from a DataTable. You will find the release on github and you are welcome to use the DynamicTable in your own projects.

https://github.com/oliverfunke/DynamicObjects/releases/tag/2014-02-16_v1.2.0

 
DynamicTable to DataTable

The following source code shows a unit test function. Within this unit test a DynamicTable will be exported as DataTable.

IDynamicTable table = new DynamicTable(DynamicTableType.Expandable);
dynamic row;
DataTable dataTable;

//add values
row = new ExpandoObject();
row.FirstName = "Hans";
row.LastName = "Mueller";
row.Age = 30;
table.AddRow(row);

row = new ExpandoObject();
row.LastName = "Meier";
row.Street = "Main street";
row.Birthday = new DateTime(2001, 12, 20);
table.AddRow(row);

//get data table
dataTable = table.AsDataTable();

 
DynamicTable from DataTable

To import a DataTable is also very easy. You may use a pre-defined DynamicTable or otherwise the columns of the DynamicTable will be created according to the DataTable content. The following source code shows an according unit test.

IDynamicTable table = new DynamicTable(DynamicTableType.Expandable);
dynamic row;

//columns
table.PreDefineColumns(
    new List<IDynamicTableColumn>()
    {
        new DynamicTableColumn<string>("FirstName"),
        new DynamicTableColumn<string>("LastName"),
        new DynamicTableColumn<int>("Age"),
        new DynamicTableColumn<DateTime>("Birthday"),
    });

//import
table.FromDataTable(...);

 
Summary

The DynamicTable object offers a flexible way to manage data within a table. By using the DataTable import and export feature you are able to exchange data between you application components ore to save the data in a file or database.

Advertisements
Dieser Beitrag wurde unter .NET, C#, DynamicObjects abgelegt und mit , , , , verschlagwortet. Setze ein Lesezeichen auf den Permalink.

Kommentar verfassen

Trage deine Daten unten ein oder klicke ein Icon um dich einzuloggen:

WordPress.com-Logo

Du kommentierst mit Deinem WordPress.com-Konto. Abmelden / Ändern )

Twitter-Bild

Du kommentierst mit Deinem Twitter-Konto. Abmelden / Ändern )

Facebook-Foto

Du kommentierst mit Deinem Facebook-Konto. Abmelden / Ändern )

Google+ Foto

Du kommentierst mit Deinem Google+-Konto. Abmelden / Ändern )

Verbinde mit %s