Binding complex virtual objects (from Entity Framework) to SfDataGrid

Hello,

I have EF objects defined as below

    public partial class Book
    {
        [Key]
        public int BookID { get; set; }
        public string BookTitle { get; set; }
        public int AuthorID { get; set; }
        public virtual Author Author { get; set; }
    }

    public partial class Author
    {
        [Key]
        public int AuthorID { get; set; }
        public string AuthorName { get; set; }

        public override string ToString()
        {
            return AuthorName;
        }
    }

When I bind the list of Book to SfDataGrid as below

sfDataGrid1.DataSource = dbContext.Book.ToList();

The data grid doesn't display the Author column. Only BookID, BookTitle and AuthorID are displayed.

In C#'s built-in DataGridView, the Author property would get its own column and ToString() is used to display to data value.

What should I do to get SfDataGrid to display the Author property (without having to manually generate extra columns)?

Thanks in advance.


12 Replies 1 reply marked as answer

DM Dhanasekar Mohanraj Syncfusion Team June 29, 2022 03:58 PM UTC

Hi Dinh,

Currently, we are checking the reported issue in our end. We will validate and update you further details on July 01, 2022.

We appreciate your patience until then,

Regards,
Dhanasekar M.



DM Dhanasekar Mohanraj Syncfusion Team July 1, 2022 02:28 PM UTC

Hi Dinh,

Your requirement to generate the columns for custom object types in SfDataGrid can be achieved by enabling SfDataGrid.AutoGenerateColumnsForCustomType property.

For more information related to, please refer the below user guide documentation link,

UG link: https://help.syncfusion.com/wpf/datagrid/columns#auto-generating-columns-for-complex-type

Please find the sample in the attachment and let us know if you have any concerns in this.

Regards,
Dhanasekar M.


Attachment: SfDataGridDemo_EF_7b0e265c.zip


DI Dinh replied to Dhanasekar Mohanraj July 4, 2022 06:15 AM UTC

Hi  Dhanasekar Mohanraj,

Thanks for the response. However, I checked and my SfDataGrid doesn't have this property

AutoGenerateColumnsModeForCustomType

It does have this one though

            sfDataGrid1.AutoGenerateColumnsForCustomType = true;

However, if I turn it on, all inner properties of the object will be generated (ie, similar to the Child mode).

I'm using Winforms, by the way (the link you posted referred to Wpf).



DM Dhanasekar Mohanraj Syncfusion Team July 5, 2022 03:18 PM UTC

Hi Dinh,

Sorry for the inconvenience caused.

Here we have attached the exact user guide documentation link in Winforms as you mentioned.

UG link: https://help.syncfusion.com/windowsforms/datagrid/columns#automatically-generating-columns

And if you need to do the same for MasterDetailsView datagrid ? You need to enable the same property for details view datagrid in its loaded event shown below,

C#:

this.sfDataGrid1.DetailsViewLoading += SfDataGrid1_DetailsViewLoading;

private void SfDataGrid1_DetailsViewLoading(object sender, Syncfusion.WinForms.DataGrid.Events.DetailsViewLoadingAndUnloadingEventArgs e)

{

    e.DetailsViewDataGrid.AutoGenerateColumnsForCustomType = true;

}


Please check this and revert us if you need further assistance for this.

Regards,
Dhanasekar M.



DI Dinh replied to Dhanasekar Mohanraj July 8, 2022 10:52 AM UTC

Hi Dhanasekar Mohanraj,

Sorry to keep troubling you on this but still, this feature is still not working yet for Winforms. To recap, according to the link you posted here

https://help.syncfusion.com/windowsforms/datagrid/columns#automatically-generating-columns

The Winforms version of SfDataGrid doesn't have this property

AutoGenerateColumnsModeForCustomType

I think there's a difference in the model between Winforms and WPF. 

Therefore, I still can't generate custom type columns (the current workaround is to do it manually).



AR Arulpriya Ramalingam Syncfusion Team July 11, 2022 03:00 PM UTC

Hi Dinh,

The WinForms SfDataGrid does support custom type column generation by enabling the AutoGenerateColumnsForCustomType property before assigning the DataSource in SfDataGrid.


We have prepared a simple sample as per your requirement to load complex property columns in SfDataGrid. Please refer to the user guide and attached sample for further reference.

UG link: https://help.syncfusion.com/windowsforms/datagrid/columns#automatically-generating-columns


Regards,

Arulpriya Ramalingam


Attachment: SyncfusionWinFormsApp2_6a2f018d.zip


DI Dinh July 18, 2022 04:59 AM UTC

Hi  Arulpriya Ramalingam,

Thanks for being patient with me. However, it's still not doing what I'm looking for. Let's say I add these 2 properties for Author.

public string Country { get; set; }
public int YOB { get; set; }

Then your Form above will also show those 2 properties on the grid. This is not the behavior that I want. What I want is a grid of Book objects that only shows properties of the Book class and if a property happens to be a complex type, then use the ToString() representation (this is the default behavior of Microsoft's DataGridView).

So using your example above, the grid would only show these 4 columns

BookID | BookTitle | AuthorID | Author 

Note: last column is Author and not Author.AuthorName because Author is the proper property name. For the Author column, the grid would use its ToString() method. I don't want the grid to show Author.Country and Author.YOB.

Is this effect achievable? Thanks again.



DM Dhanasekar Mohanraj Syncfusion Team July 19, 2022 02:16 PM UTC

Hi Dinh,


Currently, we are checking the feasibilities to achieve your requirement. We will check and update you on further details on July 21, 2022.


We appreciate your patience until then.


Regards,

Dhanasekar M.



DM Dhanasekar Mohanraj Syncfusion Team July 21, 2022 02:11 PM UTC

Hi Dinh,

We have considered your requirement of “Provide a support for AutoGenerateColumnsModeForCustomType property for SfDataGrid” and logged a feature request for the same. We will implement this feature in our upcoming Essential Studio 2022 Volume 2 Service Pack release which is expected to be rolled out at the mid of August.

You can track the status of this report through the following feedback link,

Feedback link: https://www.syncfusion.com/feedback/36481/provide-a-support-for-autogeneratecolumnsmodeforcustomtype-property-for-sfdatagrid

We appreciate your patience until then.


Regards,

Dhanasekar M.



DI Dinh July 22, 2022 01:53 AM UTC

Thank you,  Dhanasekar Mohanraj and Synfusion team. I appreciate your effort. Looking forward to the new roll out.



DM Dhanasekar Mohanraj Syncfusion Team August 8, 2022 03:35 PM UTC

Hi Dinh,


We regret for the inconvenience.


We are unable to include the fix for this issue in our Essential Studio 2022 Volume 2 SP Release which is released today as promised. We will fix this issue and include it in our upcoming Weekly NuGet release scheduled for August 16, 2022.


We appreciate your patience and understanding.


Regards,

Dhanasekar M.



DM Dhanasekar Mohanraj Syncfusion Team August 16, 2022 03:53 PM UTC

Hi Dinh,

We would like to let you know that Essential Studio Weekly NuGet packages (v20.2.0.44) have been published in nuget.org with the fix for the issue “Provide a support for AutoGenerateColumnsModeForCustomType property for SfDataGrid”.

Now you can achieve your requirement by using the below code snippet,

this.sfDataGrid1.AutoGenerateColumnsForCustomType = true;

this.sfDataGrid1.AutoGenerateColumnsModeForCustomType = AutoGenerateColumnsModeForCustomType.Parent;


We have prepared the sample based on your scenario. Please find the sample in the attachment and let us know if you have any concerns about this.

Regards,
Dhanasekar M.


If this post is helpful, please consider Accepting it as the solution so that other members can locate it more quickly.


Attachment: SfDataGridDemo_EF_f804ae39.zip

Marked as answer
Loader.
Up arrow icon