Setting columns of SfDataGrid in ViewModel

I have an ObservableCollection<MyClass> which I want to bind to a SfDataGrid, but the columns in the grid (number of columns and content) depend on which subclass of MyClass the objects in the collection are of.
I use the MVVM pattern and am quite new to it.
Is it possible to set the SfDataGrid Columns by code in the ViewModel? Or can I e.g. bind to specific properties of my class like
MappingName={Binding ...

Thanks a lot for your help.

5 Replies

JG Jai Ganesh S Syncfusion Team November 16, 2017 07:36 AM UTC

 
You can bind the SfDataGrid Columns from view model and we already have a KB article for this, 
 
 
From your query, we suspect that you want to bind the complex properties. We have prepared the sample for bind the columns which have complex properties from view model and you can refer the sample from the below location, 
 
 
Regards, 
Jai Ganesh S


AN Anne December 20, 2017 11:06 AM UTC

Thank you very much for the link and the sample, now it works!

However, when I now bind the columns, the grid suddenly cannot sort anymore (by click on the column header) though I have set AllowSorting="True". (If I set AutoGenerateColumns to True again, sorting works.) 

Thank you for your help!


JG Jai Ganesh S Syncfusion Team December 21, 2017 01:42 PM UTC

Hi Annemarie, 
We have checked your reported issue with our last attached sample but we are unable to reproduce the issue in our side. 
If your application is differing from our sample then please share the more details to reproduce this issue or if possible please share your application to reproduce the issue. This would be more helpful for us to proceed further. 
Regards, 
Jai Ganesh S


AN Anne December 27, 2017 01:12 PM UTC

I have created a sample project which reproduces the issue (no sorting possible with custom columns).
Attachment: SfGridColumnsSdadvSample_73c9cb33.zip


JG Jai Ganesh S Syncfusion Team January 2, 2018 01:52 PM UTC

Hi Annemarie,  
You can achieve your requirement to sort the columns by setting the UseBindingValue property as true for columns like below, 
protected void SetSfGridColumns() 
{ 
    SfGridColumns = new Columns(); 
    if (isCurrentlyViewingA) 
    { 
        SfGridColumns.Add(new GridTextColumn() { MappingName = "ValueA",UseBindingValue=true }); 
        SfGridColumns.Add(new GridTextColumn() { MappingName = "NumberA",UseBindingValue=true }); 
    } 
    else 
    { 
        SfGridColumns.Add(new GridTextColumn() { MappingName = "ValueB",UseBindingValue=true }); 
        SfGridColumns.Add(new GridTextColumn() { MappingName = "NumberB",UseBindingValue=true }); 
    } 
    SfGridColumns.Add(new GridTextColumn() { MappingName = "Referenced.Number",UseBindingValue=true }); 
    SfGridColumns.Add(new GridTextColumn() { MappingName = "Referenced.Date" ,UseBindingValue=true}); 
    SfGridColumns.Add(new GridTextColumn() { MappingName = "ChangedAt",UseBindingValue=true }); 
    SfGridColumns.Add(new GridTextColumn() { MappingName = "ChangedBy",UseBindingValue=true }); 
} 
 
Regards, 
Jai Ganesh S 


Loader.
Up arrow icon