I have an Datagrid which needs to get filled dynamicly.
The tablelayout is like:
id | image | name | Description | Name-1 | Name-N
2nd approach: use a DataTable. This makes use of the custom-type infrastructure under the hood, but is easier to use. Just bind the DataGrid's ItemsSource to a DataTable.DefaultView property:
This almost worked but instead of binding to the DataTable.DefaultView property property I created a property of type DataView and bound to that.
This allows the binding to be two way, binding to the DataTable.DefaultView cannot be a TwoWay binding. In the View Model
public DataView DataView
{
get { return _dataView; }
set
{
_dataView = value;
OnPropertyChanged("DataView");
}
}
With this setup I could not only define the columns dynamically when the View Model is initialized, but could update and change the data table dynamically at any time. In using the approach as defined by McGarnagle above, the view schema was not refreshing when the DataTable was updated with a new data source.