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.
Unfortunately, activation email could not send to your email. Please try again.

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

Thread ID:

Created:

Updated:

Platform:

Replies:

131548 Jul 16,2017 10:51 AM Sep 5,2017 11:52 AM WPF 12
loading
Tags: SfDataGrid
Jay Timmins
Asked On July 16, 2017 10:51 AM

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.

Jay Timmins
Replied On July 16, 2017 02:48 PM

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


Thank you.


Srinivasan Vasu [Syncfusion]
Replied On July 17, 2017 05:00 AM

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 


Jay Timmins
Replied On July 18, 2017 01:44 PM

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

Srinivasan Vasu [Syncfusion]
Replied On July 19, 2017 12:31 AM

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


Jay Timmins
Replied On July 19, 2017 03:03 AM

Looks like it got lost in the wires...

Attached


Attachment: Annotated_Example_ccac2403.zip

Jay Timmins
Replied On July 21, 2017 05:14 AM

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


Thanks


Jay Timmins
Replied On July 21, 2017 05:47 PM

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


Sivakumar R [Syncfusion]
Replied On July 22, 2017 09:50 AM

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 


Jay Timmins
Replied On July 24, 2017 05:46 AM

Dear Sivakumar,


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



Srinivasan Vasu [Syncfusion]
Replied On July 25, 2017 12:02 AM

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


Marcin Sasin
Replied On August 31, 2017 09:51 AM

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


Muthukumar Kalyanasundaram [Syncfusion]
Replied On September 5, 2017 11:52 AM

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 


CONFIRMATION

This post will be permanently deleted. Are you sure you want to continue?

Sorry, An error occured while processing your request. Please try again later.

You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.

;