Lightweight DataTable for your Silverlight applications

16. December 2009

Read this article in your language IT | EN | DE | ES

Source: Lightweight DataTable for your Silverlight Apps

Since there is no DataTable in Silverlight I’ve created small class that can be used to generate columns and rows in runtime in similar to the real DataTable way:

DataTable table = new DataTable();

table.Columns.Add(new DataColumn() { ColumnName = "ID", DataType = typeof(int) });
table.Columns.Add(new DataColumn() { ColumnName = "Name", DataType = typeof(string) });
table.Columns.Add(new DataColumn() { ColumnName = "UnitPrice", DataType = typeof(decimal) });
table.Columns.Add(new DataColumn() { ColumnName = "Date", DataType = typeof(DateTime) });
table.Columns.Add(new DataColumn() { ColumnName = "Discontinued", DataType = typeof(bool) });

for(var i = 0; i < 1000; i++)
{
    DataRow row = new DataRow();
    row["ID"] = i;
    row["Name"] = names[rnd.Next(9)];
    row["UnitPrice"] = prizes[rnd.Next(9)];
    row["Date"] = DateTime.Now.AddDays(i);
    row["Discontinued"] = bools[rnd.Next(9)];

    table.Rows.Add(row);
}

Class File is named Data.cs, c3 Version: 


Unfortunately, there’s no VB.Net version of this, haven't gotten to it yet.

Code Snippets, SilverLight , ,

Comments

8/12/2010 11:16:48 PM #
good post