Process custom property types in AutoGeneratingColumn

I'm looking to process a custom object type as a ComboBox control, but I cannot figure out how to query the reflected types when the grid auto generates the columns (SfDataGrid.AutoGenerateColumn event).


Is there a way to get the reflected type, or another way to implement a control over a custom type/class? I looked through the documentation, but in the mass of information I couldn't find what I was looking for.


In the case that I'm looking in the wrong direction here, my end goal is to have every property of type Ref<T> be automatically processed as a ComboBox bound to another ObservableCollection<T>.


11 Replies 1 reply marked as answer

SB Sweatha Bharathi Syncfusion Team June 6, 2023 01:23 PM UTC

Hi Dan ,


Your requirement, "Process custom property types in AutoGeneratingColumn," can be achieved by using the AutoGenerateColumnsForCustomType property and AutoGeneratingColumn event. We have prepared a sample based on your requirements. Please review the sample and let us know if any further assistance is required.


UG link: https://help.syncfusion.com/windowsforms/datagrid/columns


Attachment: SfDataGridGetCustomProperty_ab539b7d.zip


DA Dan June 11, 2023 09:00 AM UTC

I need a way to check against the type of the source property, rather than the mapping name. Is there a way to achieve this?



SB Sweatha Bharathi Syncfusion Team June 12, 2023 02:34 PM UTC


Dan ,


We have prepared a sample based on your requirements. Please review the sample and let us know if any further assistance is required.


Attachment: SfDataGridGetCustomProperty_9518596c.zip


DA Dan June 12, 2023 08:24 PM UTC

Thanks for the sample, I can get most of the way using this approach.

However, I cannot seem to bind the ItemsSource or SelectedItem of the GridComboBoxColumn. Is there another way to bind these, or otherwise implement a custom IValueConverter to convert the ItemsSource values to the property type?



SB Sweatha Bharathi Syncfusion Team June 13, 2023 02:14 PM UTC

Dan ,


We are a little unclear about your requirements. If you are trying to bind an item source based on property type,  kindly share more details about it. This would be helpful in proceeding further."



DA Dan June 13, 2023 11:54 PM UTC

The ItemsSource of the combo box is a ObservableCollection<T>, and the SelectedItem (underlying property) is a Ref<T>. Currently SelectedItem is expected to be of type T rather than Ref<T>.

So I am wondering if it's possible to make the GridComboBoxColumn use an IValueConverter that will convert T to Ref<T> and back.



SB Sweatha Bharathi Syncfusion Team June 14, 2023 01:06 PM UTC

Dan ,


We are  checking your reported problem with provided information and we need a time to validate this. We will update you with further details on , June 16 2023.



SB Sweatha Bharathi Syncfusion Team June 16, 2023 10:26 AM UTC

Dan ,

We are still checking your reported problem with provided information and we need a time to validate this. We will update you with further details on , June 20 2023.



SB Sweatha Bharathi Syncfusion Team June 20, 2023 02:08 PM UTC

Hi Dan,


You can achieve your requirement of changing the SelectedItem type to the GridComboBoxColumn's ItemsSource type by using the ValueBinding property with a Converter. Please refer to the code snippet below:

private void Datagrid_AutoGeneratingColumn(object sender, Syncfusion.UI.Xaml.Grid.AutoGeneratingColumnArgs e)

        {

 

            var propertyType = GetTypeOfProperty(e.Column.MappingName, typeof(Person));

 

            if (e.Column.MappingName == "Gender")

            {

 

                if (propertyType == typeof(string))

                {

                    e.Column = new GridComboBoxColumn()

                    {

                        UseBindingValue = true,

                        MappingName = "Gender",

                        ItemsSource = GetComboxItems(),

                        DisplayMemberPath = "GenderType",

                        SelectedValuePath = "GenderType",

                        IsEditable = true,

                        ValueBinding = new Binding("Gender")

                        {

                            Converter = new ComboBoxSelectedValueConverter()

                        },

                    };

                }

            }

        }


public class ComboBoxSelectedValueConverter : IValueConverter

    {

        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)

        {

            if (value != null)

            {

                var gender = new GenderClass() { GenderType = value.ToString() };

                return gender.GenderType;

            }

            else

                return value;

        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)

        {           

            return value;

        }

    }


To update the GridComboBoxColumn's SelectedItem to be displayed in the DataGrid's cell, you can set the corresponding type to the SelectedValuePath property of the GridComboBoxColumn. For a better understanding, please refer to the attached sample.

If the above updates do not meet your requirement, please provide more details for further analysis:

  1. Could you clarify how you are setting the SelectedItems for the GridComboBoxColumn?
  2. Could you provide any image references for better illustration?

This additional information will assist us in investigating the matter promptly.



Attachment: SfDataGridGetCustomProperty_3ceb4c9d.zip

Marked as answer

DA Dan June 23, 2023 04:52 AM UTC

That 



SB Sweatha Bharathi Syncfusion Team June 23, 2023 12:06 PM UTC



Dan ,


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 you😊.


Loader.
Up arrow icon