We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date
close icon

How to bind a GridComboBoxColumn to a property of the element in the SfDataGrid collection?

It seems from the documentation and from my attempts that the GridComboBoxColumn and GridMultiColumnDropDownList have a DataContext of the root element, and not the SfDataGrid itself. How can you bind the column ItemsSource to a collection property owned by the object behind the record (an element of the grid collection)?
e.g.
(Window DataContext is the ViewModel)
ItemsSource="{Binding VMColl}"> (A collection property exposed in the viewmodel)
ItemsSource="{Binding VMCollProp}"/> (This gives a databinding error saying it can't find this property on the ViewModel itself)

I've tried binding the SelectedItem of the sfDataGrid to a property in the view model, then declaring two properties which get the collection members from the selected item, and binding the ItemsSource to those. This is quite hacky and messy and doesn't work with GridMultiColumnDropDownList though.

12 Replies

JT Jay Timmins July 16, 2017 06:48 PM UTC

Looks like the formatting lost some of my code example. It is meant to read:


Thank you.



SV Srinivasan Vasu Syncfusion Team July 17, 2017 09:00 AM UTC

Hi Jay, 
 
Thanks for contacting Syncfusion support. 
 
We have checked your query and we have prepared a sample as per your requirement. The mentioned issue reproduced only when the bounded property not available in ViewModel and we did not reproduce the reported issue in our side. In sample, ComboItems property bound with GridComboBoxColumn which is declared in ViewModel. You can bound the collection property with GridMultiColumnDropDownList  column in the same way. 
 
Please refer the below code example. 
 
<Window.Resources> 
        <local:ViewModel x:Key="ordercollection"/> 
    </Window.Resources> 
 
<syncfusion:GridComboBoxColumn HeaderText="Country" 
                                               ItemsSource="{Binding ComboItems,Source={StaticResource ordercollection}}" 
                                               MappingName="Country"/> 
 
Please download the sample from the below link. 
 
 
If the issue still persists, please revert us the modified sample with the replication procedure. 
 
If your requirement is different from this, please provide more details and modified sample. It will be helpful to provide better support.  
 
 
 
Regards, 
Srinivasan 



JT Jay Timmins replied to Srinivasan Vasu July 18, 2017 05:44 PM UTC

Dear Srinivasan,Thanks for your response. Unfortunately in your sample this shows binding to a collection which is on the ViewModel. My collection is a property of the model. In all of the columns in your example, you are binding to a property of the Model itself, e.g. OrderID, CustomerName. I attached the sample - I made adjustments to the ViewModel and Model files with comments to show what I mean. Consider a collection property exposed on the model - is there a way to represent this as a GridComboBoxColumn, or a GridMultiColumnDropDownList so that it can be edited?Attachment: Annotated_Example_9a459312.zip


SV Srinivasan Vasu Syncfusion Team July 19, 2017 04:31 AM UTC

Hi Jay, 
  
We cannot download the sample from the attachment. Please provide proper attached file. 
  
Regards, 
Srinivasan 



JT Jay Timmins July 19, 2017 07:03 AM UTC

Looks like it got lost in the wires...

Attached


Attachment: Annotated_Example_ccac2403.zip


JT Jay Timmins July 21, 2017 09:14 AM UTC

Any luck with this, or is it not possible to do this with the DataGrid?


Thanks



JT Jay Timmins July 21, 2017 09:47 PM UTC

Nevermind, I addressed this issue by using a GridTemplateColumn which actually binds to the properties on the model itself.


If anyone is in a similar situation and wanted to see my solution, feel free to drop me a message



SR Sivakumar R Syncfusion Team July 22, 2017 01:50 PM UTC

Hi Jay, 

Sorry for the delay. We worked in creating custom column to meet your requirement for better performance over TemplateColumn. 

You can use Template column to achieve your requirement like you are doing already. 

I would suggest to use the custom column created in the below sample for better performance over TemplateColumn. 

<local:GridComboBoxColumnExt MappingName="OrderValue"> 
    <local:GridComboBoxColumnExt.ItemsSourceSelectorSelector> 
        <local:ItemsSourceSelectorSelector/> 
    </local:GridComboBoxColumnExt.ItemsSourceSelectorSelector> 
</local:GridComboBoxColumnExt> 
 
public class ItemsSourceSelectorSelector : IItemsSourceSelector 
{ 
    public IEnumerable GetItemsSource(object record, object dataContext) 
    { 
        if (record == null) 
            return null; 
 
        var model = record as Model; 
        return model.OrderValues; 
    } 
} 

Below sample returns different ItemsSource for each row from data object associated with row. 

Below sample filters and returns different ItemsSource for each row from ViewModel. 

Thanks, 
Sivakumar 



JT Jay Timmins July 24, 2017 09:46 AM UTC

Dear Sivakumar,


Thanks for the great support! I'll play with your solution as I'm sure it's more performant.




SV Srinivasan Vasu Syncfusion Team July 25, 2017 04:02 AM UTC

Hi Jay, 
  
Thanks for your update. 
  
Please let us know if you need further assistance. 
  
Regards, 
Srinivasan 



MS Marcin Sasin August 31, 2017 01:51 PM UTC

Hello.


It is extremely "un-intuitive" that You can't simply bind row element to underlying row data. Especcially it is working fine with MasterDetail part of SfDatagrid. Ok You're using RelationalColumn parameter, but that's logical. Why not in ComboboxColumn? I know it can be usefull, but shouldn't be a dead end.


Marcin



MK Muthukumar Kalyanasundaram Syncfusion Team September 5, 2017 03:52 PM UTC

Hi Marcin, 
 
Thanks for the update. 
 
We have created a new incident under your account related to this query. Please log on to our support website to check for further updates,  
 
 
Please let me know if you have any questions.  
 
Regards, 
Muthukumar K 


Loader.
Live Chat Icon For mobile
Up arrow icon