Gridview enum wrapper in ComboBoxColumn

1 Answer 52 Views
GridView
Daniel
Top achievements
Rank 2
Iron
Iron
Daniel asked on 09 Sep 2024, 02:52 PM

Hi Telerik support team,

I’ve a question about the GridView for Winforms. In my grid I’ve a combobox column which is based on a enum. Before, in net framework 4.7.2 I had a nice wrapper around it in order to translate the values. Works like a charm.

Now we’ve updated our application to net8 and that part does not work anymore. I’ve isolated the code in a simple example program. The attached solution contains 2 projects, one is net framework and the other is net8. Code is the same. In the net8 I get an exception when I try to set a filter on the column.

Are you aware of this? Did I miss something? How can I get this working in net8?

Any help is welcome.

Regards,

Daniel Kaya

1 Answer, 1 is accepted

Sort by
0
Accepted
Dinko | Tech Support Engineer
Telerik team
answered on 12 Sep 2024, 11:17 AM

Hello Daniel,

The provided project is greatly appreciated. 

I was able to observe the described behavior. The error is thrown in the EnumConverter.ConvertTo() method. It seems that Microsoft has made some changes with the introduction of .Net Core. Upon investigating the error the EnumConverter fails to convert the "Geen" string to BatchStatus. The string is not a member of the custom enum thus leading to the error. The DataType of the column is pointing to the enum. To be honest I think in general such error should be thrown. My opinion is that this is how it should work. Not sure why it was working in the old version. However, this is quite interesting behavior. I have searched the web but could not find anything related to the EnumConverter in .Net Core. If I manage to find something related to this I will contact you again.

Now to make it work in .Net 8, you explicitly point the column to use TypeConverter instead of EnumConverter.

private void BuildStatusColumnSource()
{
    var column = (GridViewComboBoxColumn)radGridView1.Columns["Status"];
    var source = new BindingList<GridViewEnumHandler>();

    //  column.FilteringMode = GridViewFilteringMode.DisplayMember;
    source.Add(new GridViewEnumHandler(BatchStatus.None, () => "Geen"));
    source.Add(new GridViewEnumHandler(BatchStatus.Prepared, () => "Voorbereid"));
    source.Add(new GridViewEnumHandler(BatchStatus.Production, () => "Productie"));
    source.Add(new GridViewEnumHandler(BatchStatus.Done, () => "Klaar"));
    source.Add(new GridViewEnumHandler(BatchStatus.Cancelled, () => "Geannuleerd"));

    column.DataSource = source;
    column.DisplayMember = "EnumDisplay";
    column.ValueMember = "EnumValue";
    column.DataTypeConverter = new TypeConverter();
    column.DataType = typeof(BatchStatus);
}

This way the error is not raised anymore. I hope that you can use this approach in your application.

Regards,
Dinko | Tech Support Engineer
Progress Telerik

Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.

Tags
GridView
Asked by
Daniel
Top achievements
Rank 2
Iron
Iron
Answers by
Dinko | Tech Support Engineer
Telerik team
Share this question
or