DynamicTable: create a dynamic and expandable table

During the last projects I have implemented a lot of data classes. These data classes where sometimes only used for a single purpose and only within a very small code area. For example I want to store the actual status and settings of a graphical user interface. In this case I often create one settings data class per view. These classes may be easily serialized with a XML file. This is a fast to implement and very efficient way to store the data. But a lot of data classes must be implemented, which is a very boring routine work.

Maybe you know this from you own software projects and you also often thought that there must be a better solution. In this article I want to show you how dynamic objects will make the boring implementation of data classes obsolete.

In a previously article I wrote about the ExpandoObject class. As I had have seen the nice dynamic features of this class I got the idea to implement some basic dynamic objects. Therefore I have started a new project on github, the DynamicObjects.

Actual the implementation of the first dynamic object is finished. It is a dynamic and expandable data class to store a table. You will find the release on github and you are welcome to use it in your own projects.


Create a dynamic table

The following source code shows an example how a table may be created and used.

    1 IDynamicTable table = new DynamicTable(DynamicTableType.Expandeable);           

    2 dynamic row;


    4 row = new ExpandoObject();

    5 row.FirstName = "John";

    6 row.LastName = "Doe";

    7 row.Age = 30;

    8 table.AddRow(row);


   10 row = new ExpandoObject();

   11 row.FirstName = "Jane";

   12 row.LastName = "Doe";

   13 row.Street = "Main street";

   14 table.AddRow(row);


   16 foreach (dynamic actualRow in table.Rows)

   17 {

   18     Console.WriteLine(

   19         string.Format("{0} {1} is {2} years old.",

   20             actualRow.FirstName,

   21             actualRow.LastName,

   22             actualRow.Age));

   23 }

In line 1 the table will be created. The enumerator value Expandable means that the table will be expanded automatically depending on the elements of the rows. It is also possible to use a well formed table with pre-defined columns.

In lines 4 to 8 the first row will be created and added to the table. The row may contain any elements of your choice. There is no need to define a data class with the elements FirstName, LastName and Age.

At next the second row will be added to the table. The second row does not contain a value for the element Age. Therefore this element will be added automatically with the default value of this data type, in this case 0. Furthermore the row contains the new element Street. Therefore the whole table will be expanded by a new column.

In line 16 to 23 the table will be accessed. You see, that all elements are accessible, even these elements which were not created explicitly, e.g. the Age element of the second row.

CSV export and import

The content of the dynamic table can be exported to a CSV file. The following source code shows how to export a table.

    1 using (StreamWriter writer = new StreamWriter("test.csv"))

    2 {

    3     writer.Write(table.AsCsv());

    4 }

To import a table is nearly that simple like to export one. But in this case the columns must be pre- defined. This is necessary to know the type of all elements.

    1 private static void ImportCsvFile()

    2 {           

    3     IDynamicTable table = new DynamicTable(DynamicTableType.Expandeable);


    5     //pre define columns

    6     table.PreDefineColumns(

    7         new List<IDynamicTableColumn>()

    8         {

    9             new DynamicTableColumn<string>("FirstName"),

   10             new DynamicTableColumn<string>("LastName"),

   11             new DynamicTableColumn<int>("Age"),

   12             new DynamicTableColumn<string>("Street")

   13         });


   15     //import

   16     using (StreamReader reader = new StreamReader("test.csv"))

   17     {

   18         table.FromCsv(ReadFile(reader));

   19     }

   20 }


   22 private static IEnumerable<string> ReadFile(StreamReader reader)

   23 {

   24     while (reader.EndOfStream == false)

   25     {

   26         yield return reader.ReadLine();

   27     }

   28 }


The DynamicTable object offers a very efficient way to manage data within a table. By using the   DynamicTable it may be possible to reduce the number of needed data classes within a software application. This will speed up the implementation process and reduces the boring routine work to create data classes.

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

2 Antworten zu DynamicTable: create a dynamic and expandable table

  1. Pingback: DynamicTable to XML | coders corner

  2. Pingback: DynamicTable to DataTable | coders corner

Kommentar verfassen

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


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


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


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

Google+ Foto

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

Verbinde mit %s