Lightweight DataTable for Silverlight applications

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:

using System;
using System.Windows;
using SilverlightDataTable.DataTable;

namespace SilverlightDataTable
{
    public partial class MainPage
    {
        string[] _names = new string[] { "Côte de Blaye", "Boston Crab Meat", 
            "Singaporean Hokkien Fried Mee", "Gula Malacca", "Rogede sild", 
            "Spegesild", "Zaanse koeken", "Chocolade", "Maxilaku", "Valkoinen suklaa" };
        decimal[] _prizes = new decimal[] { 23.25M, 9.00M, 45.60M, 32.00M, 
            14.00M, 19.00M, 263.50M, 18.40M, 3.00M, 14.00M };
        bool[] _bools = new bool[] { true, false, true, false, true, false, true, false, true, false };
        DataTable.DataTable _table = null;

        private readonly Random _rnd = new Random();

        public MainPage()
        {
            InitializeComponent();
            Loaded += Page_Loaded;
        }
       private void Page_Loaded(object sender, RoutedEventArgs e)
        {
            _table = new DataTable.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 (int i = 0; i < 5; i++)
            {
                var row = _table.NewRow();
                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);
            }


            DataContext = _table;
        }

        private void btnAdd_Click(object sender, RoutedEventArgs e)
        {
            var table = (DataTable.DataTable)DataContext;

            var row = table.NewRow();
            row["ID"] = table.Rows.Count;
            row["Name"] = _names[_rnd.Next(9)];
            row["UnitPrice"] = _prizes[_rnd.Next(9)];
            row["Date"] = DateTime.Now.AddDays(table.Rows.Count);
            row["Discontinued"] = _bools[_rnd.Next(9)];
            table.Rows.Add(row);
        }

        private void btnDelete_Click(object sender, RoutedEventArgs e)
        {
            var table = (DataTable.DataTable)DataContext;
            table.Rows.Remove(table.Rows[dataGrid1.SelectedIndex]);
        }
    }
}

 I have attached the sample application.

SilverlightDataTable.zip (253.24 kb)

Posted by: Mohd Ishaq

Categories: C#, Silverlight

Tags: ,

Comments (1) -

allou Cambodia

February 3. 2012 07:56

allou
ça marche à merveille!! Thanks!!

Add comment

  Select your country

biuquote
  • Comment
  • Preview
Loading