ComboBox with KeyValuePair<string, string> with EnableVirtualization throw cast is not valid

Hi,


When the DataSource of a ComboBox is a List of KeyValuePair<string, string> the component stops working if we enable virtualization.


It's working with a List of a complex object, but with a  KeyValuePair<string, string> it's throwing a 'cast is not valid' error.


Please see the attached zip.


Attachment: SyncfusionComboboxKVP_caf02332.zip

4 Replies 1 reply marked as answer

PK Priyanka Karthikeyan Syncfusion Team August 10, 2023 11:18 AM UTC

Hi Julien Barach,

Thanks for contacting syncfusion.

To address this, you could create a custom data model to encapsulate the KeyValuePair<string, string> data, which might help to resolve the casting error. Here's an example of how you could modify your code:

<SfComboBox TValue="string"

            TItem="CustomDataModel"

            DataSource="@datasource"

            EnableVirtualization="true">

    <ComboBoxFieldSettings Text="Value" Value="Key"></ComboBoxFieldSettings>

</SfComboBox>


@code {

    private List<CustomDataModel> datasource = new() {

        new CustomDataModel("ID1", "VALUE1"),

        new CustomDataModel("ID2", "VALUE2"),

        new CustomDataModel("ID3", "VALUE3")

    };


    public class CustomDataModel

    {

        public string Key { get; set; }

        public string Value { get; set; }


        public CustomDataModel(string key, string value)

        {

            Key = key;

            Value = value;

        }

    }

}



Please find the attached sample below for your reference.

If you need any further assistance, please let us know. We are happy to assist you further.

Regards,

Priyanka K



Attachment: SyncfusionComboboxKVP_modified_f328b9a6.zip


JB Julien Barach August 10, 2023 11:33 AM UTC

Hi,


Indeed, I knew how to workaround it.

But now my question is why it's not working with a KVP ? Is it a bug you are going to fix ?

It's not convenient to live with it and always go for this workaround.


On the Dropdown component it's working with a KVP. So it would be great if it also works with ComboBox.


Regards,

Julien



KP Kokila Poovendran Syncfusion Team August 17, 2023 02:39 PM UTC

Hi Julien Barach,


We have considered the reported issue ““Cast is not valid” error has thrown when we bind TItem as KeyValuePair<string, string>” as a bug from our end and the fix for the issue will be included with our upcoming patch release on August 30, 2023.



Now you can track the status of the reported issue through the feedback below,


Feedback Link:https://www.syncfusion.com/feedback/46195/-cast-is-not-valid-error-has-thrown-when-we-bind-titem-as-keyvaluepairstring


Until then, we suggest a workaround solution: you can remove 'EnableVirtualization="true"' from the code to resolve this issue.


Disclaimer: “Inclusion of this solution in the weekly release may change due to other factors including but not limited to QA checks and works reprioritization.”




KP Kokila Poovendran Syncfusion Team September 19, 2023 05:02 PM UTC

Hi Julien Barach,


We sincerely apologizes for inconvenience caused. We have included the fix for the issue "Cast is not valid” error has thrown when we bind TItem as KeyValuePair<string, string>" with our package version “23.1.36”. Therefore, we recommend upgrading to our latest version to resolve the current issue.


 

Regards,

Kokila Poovendran.


Marked as answer
Loader.
Up arrow icon