Your ItemsSource would need to be a List of objects, not a DataRow/Column. In any case referencing these sorts of things on the ViewModel breaks MVVM!
So:
public List<Result> TableResult {get;set;} // raise property change on set
and
public class Result
{
public string Color {get;set;}
public int Number {get;set;}
}
EDIT: Since you don't know anything about the table itself beforehand, I suggest you use a simple List of List of Strings to produce your rows and columns.
Something like this:
public List<List<string>> TableResult { get; set; } // raise property changed
and
List<string> columns = new List<string>() { "Col1", "Col2", "Col3", "Col4" };
List<string> row1 = new List<string>() { "a", "b", "c", "d" };
List<string> row2 = new List<string>() { "w", "x", "y", "z" };
List<List<string>> table = new List<List<string>>(){columns,row1,row2};
TableResult = table;
You then must create a converter to convert your rows into a DataTable:
public class ListToTables : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
var list = value as List<List<string>>;
var headers = list.First();
DataTable dt = new DataTable();
foreach (string header in headers)
{
dt.Columns.Add(header);
}
foreach (List<string> row in list.Skip(1))
{
int index = 0;
DataRow r = dt.NewRow();
foreach (string col in row)
{
r[index++] = col;
}
dt.Rows.Add(r);
}
return dt;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotSupportedException();
}
}
on your XAML, add this to your resources (e.g. UserControl.Resources):
<local:ListToTables x:Key="listToTables"></local:ListToTables>
where "local" is the added namespace of your converter, and:
<DataGrid ItemsSource="{Binding TableResult,Converter={StaticResource listToTables}}" >
for your DataGrid.
EDIT FINAL:
You can keep your DataTable if you believe MVVM allows it, just replace the latter part of your code with this:
DataTable tempDataTable = new DataTable();
DataColumn c = new DataColumn();
c.ColumnName = "Col1";
tempDataTable.Columns.Add(c);
c = new DataColumn();
c.ColumnName = "Col2";
tempDataTable.Columns.Add(c);
DataRow row1 = tempDataTable.NewRow();
row1["Col1"] = "Blue";
row1["Col2"] = "12";;
tempDataTable.Rows.Add(row1);
DataRow row2 = tempDataTable.NewRow();
row2["Col1"] = "Red";
row2["Col2"] = "18";
tempDataTable.Rows.Add(row2);
DataRow row3 = tempDataTable.NewRow();
row3["Col1"] = "Yellow";
row3["Col2"] = "27";
tempDataTable.Rows.Add(row3);
this.DataTable = tempDataTable;
You have to set the DataTable to raise property changed. Adding to it won't do anything ;)