using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;

namespace DataTableGrouping
{
    public partial class MainPage : UserControl
    {
        public MainPage()
        {
            InitializeComponent();

            // create table
            var dt = new C1.Silverlight.Data.DataTable();
            dt.Columns.Add("Date", typeof(DateTime));
            dt.Columns.Add("Country", typeof(string));
            dt.Columns.Add("Product", typeof(string));
            dt.Columns.Add("Amount", typeof(double));

            // populate table
            var rnd = new Random();
            var countries = "Austria|Belgium|Chile|Denmark|Egypt|Finland|Greece|Honduras|India|Japan".Split('|');
            var products = "WPF|SL|ASP|WF|CF|Metro".Split('|');
            for (int i = 0; i < 1000; i++)
            {
                var row = dt.NewRow();
                row["Date"] = DateTime.Today.AddDays(rnd.Next(-3000, 0));
                row["Country"] = countries[rnd.Next(0, countries.Length)];
                row["Product"] = products[rnd.Next(0, products.Length)];
                row["Amount"] = rnd.Next(1000, 10000);
                dt.Rows.Add(row);
            }

            // add grouping to the table's default view
            var gd = dt.DefaultView.GroupDescriptions;
            gd.Add(new PropertyGroupDescription("Country"));
            gd.Add(new PropertyGroupDescription("Product"));

            // show view
            var f = new C1.Silverlight.FlexGrid.C1FlexGrid();
            f.ItemsSource = dt.DefaultView;
            LayoutRoot.Children.Add(f);
        }
    }
}
