ComboBoxAdv as DataTemplate in a SfDataGrid GridTemplateColumn.

Hello,

I'm encountering a lot of issues trying to use a ComboBoxAdv as DataTemplate in a SfDataGrid GridTemplateColumn.

What I would like to achieve is to assign multiple countries to each person.


To do this, I would like to:

1. Use a list of countries as the ItemSource of the ComboBoxAdv.

2. Use the Country.Id field as the SelectedValuePath.

3. Use the Country.Name field as the DisplayMemberPath.

4. Assign the selected Ids to Person.CountriesId collection.


I have made many attempts but each time there is a different problem which I don't understand why


Thanks

Michele


Attachment: TestComboAdvSln_edd956fe.zip

5 Replies

SG Santhosh Govindasamy Syncfusion Team October 5, 2023 04:57 PM UTC

Hi Michele,

We have investigated the reported issue and have successfully replicated it on our end. You are facing an ArgumentException, and the issue occurs when binding the "CountriesId" collection to "SelectedItems." You initialized "CountriesId" to be an Integer type collection, but "SelectedItems" carries an object type collection.

 

To resolve the issue, change the collection type of "CountriesId" from integer to object type, and then set the "MappingName" to "GridTemplateColumn." The correct mapping name value is "Id."


Here the Code snippet for your reference.

public ObservableCollection<object> CountriesId = new ObservableCollection<object>();


<syncfusion:GridTemplateColumn HeaderText="Countries" MappingName="Id">

                    <syncfusion:GridTemplateColumn.CellTemplate>

                        <DataTemplate>

                            <Grid>

                                <syncfusion:ComboBoxAdv

                                    Height="30"

                                    AllowMultiSelect="True"

                                    DisplayMemberPath="Name"

                                    EnableOKCancel="True"


                                    EnableToken="True"

                                    IsEditable="True"

                                    ItemsSource="{Binding DataContext.AllCountries, RelativeSource={RelativeSource AncestorType=Window}}"

                                    SelectedItems="{Binding CountriesId,Converter={StaticResource countriesConverter}}"

                                    SelectedValuePath="Id" />

                            </Grid>

                        </DataTemplate>

                    </syncfusion:GridTemplateColumn.CellTemplate>

                </syncfusion:GridTemplateColumn>



Regards,
Santhosh.G



MI Michele October 6, 2023 06:43 AM UTC

Hi  Santhosh

Still not working.

Exceptions.JPG


Please, can you send me back the project with your working solution implemented?


Michele




MI Michele October 6, 2023 07:55 AM UTC

I've modified my code implementing Models.Person to read json file and Entities.Person to match Country.Id to Country

I think that in SF code behind it try to instantiatiate an object and Int can not be istantiated. 

This behaviour causes an exception.


I've try to use an SfComboBoxAdv alone, not in a SfDataGrid, and bound SelectedItems to DataContext.SelectedPerson.PersonCountries.


It works once but when i select another Person, SelectedItems in SfCBA doesn't update the list.


So, i try to implement a SelectedCountries in ViewModel and bound it to ItemsSource of SfCBA.

So when i switch SelectedPerson, each Country in SelectedPerson.PersonCountries it is added to  SelectedCountries and UI is updated correctly.

This works.


This means that in code behind of SfCBA the binding of SelectedItems is not well menaged. 

Not as a developer would expect.


I think this is a Bug


So, how can i implement a ComboBoxAdv as a CellTemplate in a SfDataGrid using the code provided initially?


Michele





MI Michele October 6, 2023 12:23 PM UTC

Problem solved

The trick is to create a UserControl that wrap ComboBoxAdv and use DependecyProperties to bind SelectedItems for each Person


Thanks

Michele



SG Santhosh Govindasamy Syncfusion Team October 9, 2023 12:48 PM UTC

Michele,

We are glad to know that the reported problem has been resolved at your end. Please let us know if you have any further queries on this. We are happy to help.


Loader.
Up arrow icon