Dynamic Data binding in WPF Devx GridControl

Sennin picture Sennin · Apr 26, 2011 · Viewed 8.5k times · Source

I'm working on a WPF application ( MVVM) and using the DevExpress GridCOntrol. I need to create a generic screen to display data from multiple tables ( displaying one at a time) which have reference data. So the grid control needs to bind to a dataset which can contain differnt number of columns depending on the table being queries.

Questions:

1) What type of object should my data access layer return? At present I can only think of a Dataset /DataTable.. is there any other alternative as I really want to avoid using datasets and datatables .. a dictionay perhaps ? Whats the best way to return such data ?

2) In case I return something besides a Dataset/DataTable , how do I bind my GridControl with this dynamic data collection ?

Thanks a lot.

Answer

kenwarner picture kenwarner · Apr 26, 2011

I've used an approach similar to this in the past with success

http://www.paulstovell.com/dynamic-datagrid

public class Property : INotifyPropertyChanged
{
    public Property(string name, object value)
    {
        Name = name;
        Value = value;
    }

    public string Name { get; private set; }
    public object Value { get; set; }
}


public class Record
{
    private readonly ObservableCollection<Property> properties = new ObservableCollection<Property>();

    public Record(params Property[] properties)
    {
        foreach (var property in properties)
            Properties.Add(property);
    }

    public ObservableCollection<Property> Properties
    {
        get { return properties; }
    }
}
<DataGrid 
   Name="dataGrid" 
   AutoGenerateColumns="false" 
   ItemsSource="{Binding Path=Records}"/>