Datagrid binding Dictionary

Hi,

my ViewModel  contains the following Dictionary:

private Dictionary<string, ObservableCollection<string>> dataDictionary = new Dictionary<string, ObservableCollection<string>>();
public Dictionary<string, ObservableCollection<string>> DataDictionary {
    get {return dataDictionary; }
    set {
        dataDictionary = value;
        OnPropertyChanged(nameof(DataDictionary));
    }

}

The dataDictionary is filled by the following method:

private void LoadDictionary() {
    Dictionary<string, ObservableCollection<string>> data = new Dictionary<string, ObservableCollection<string>>();
    data["TimeStamp"] = new ObservableCollection<string>();
    for (int i = 0; i < recordingData.Data.Channels.Length; i++) {
        data[recordingData.MetaData.Channels[i].ChannelId] = new ObservableCollection<string>();//In my Sample I have to ChannelIds "A","C" 
    }

    for (int i = 0; i < recordingData.MetaData.Common.RecordCount; i++) {
        data["TimeStamp"].Add(i.ToString());
        for (int j = 0; j < recordingData.Data.Channels.Length; j++) {
            data[recordingData.MetaData.Channels[j].ChannelId].Add(recordingData.Data.Channels[j].Values[i].ToString());//Channels contains several thousand double values
        }

    }
    dataDictionary = data;
}

Then I have a page that contains the grid:

<syncfusion:SfDataGrid x:Name="grid" Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="2"/>

In the codebehind I am trying to set the data:

this.viewModel = viewModel;
grid.ColumnSizer = ColumnSizer.Star;
grid.AutoGenerateColumns = false;
foreach (var key in viewModel.DataDictionary.Keys) {
    GridTextColumn col = new GridTextColumn();
    col.HeaderText = key;
    col.MappingName = $"DataDictionary[{key}]";
    grid.Columns.Add(col);
}
grid.ItemsSource = viewModel.DataDictionary;


The grid is displayed with the right columns but the data from the ObservableCollections is not shown.

I see only three empty line (I guess this is caused by the dictionary that is containing three keys).

What do I have to change to get the data from the ObservableCollections displayed?


Attached you find a screenshot of my grid.


Thanks


Jens




Attachment: Screenshot_Grid_9c237462.zip

3 Replies

AN Ashok N Syncfusion Team June 13, 2018 12:37 PM UTC

Hi Jens, 
 
Thanks for contacting Syncfusion support. 
 
We have checked your query and using below KB link we can able to bind the Dictionary to our SfDataGrid. using your code example we cannot able to understand your record adding to ItemsSource and Model property type (GridColumn MappingName type). So, we request you to share your sample along with replication procedure or please check the below KB link for Dictionary binding in our SfDataGrid.  
 
 
Regards, 
Ashok 



JB Jens Bohrmann June 19, 2018 12:46 PM UTC

Ok, I found my problem.
I had a Dictionary containing ObservableCollections for each Column.
In your KB link it is done the other way arround. It has a ObservableCollection containing objects with Dictionaries for each dataset.

I changed so it is working now.


AN Ashok N Syncfusion Team June 20, 2018 03:51 AM UTC

Hi Jens,  
 
Thanks for your update. Please let us know if you require further assistance on this .  
 
Regards,  
Ashok  


Loader.
Up arrow icon