Is it possible to name autogenereated column headers using a decorator and not the name of the property in the model?

I'm using an SfDataGrid control to display some data coming from an ObservableCollection. ​Columns are getting autogenerated and named based on the properties of the model (i.e. first name, last name, phone). Can I change the name displayed in the column header without changing the name of the underlying model property? For example if the property in the model is called FirstName​ can I add a decorator/attribute to let the control know the column name must be First Name​?


3 Replies 1 reply marked as answer

KK Karthikraja Kalaimani Syncfusion Team May 12, 2023 09:19 AM UTC

Hi Nicholas,

Yes, you can change the column header text without changing the name of the underlying model property in the SfDataGrid. You can use the AutoGeneratingColumn event of the SfDataGrid control to customize the column header text based on your requirements. Please refer to the following code snippets. 

Code snippet : 
 private void datagrid_AutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
    {
       // set the header text of the column based on the property name
        if(e.Column.MappingName == "OrderID")
        {
            e.Column.HeaderText = "Order ID";
        }
    }

For more details, please refer to the following UG guide lines to know about AutoGeneratingColumn event. 
https://help.syncfusion.com/maui/datagrid/columns#customize-automatically-generated-columns

Regards,
Karthik Raja

Marked as answer

NI Nicholas May 12, 2023 09:50 AM UTC

Thanks a lot!

Would it be possible to request the same feature for a future update but in the form of decorators/attributes on the model class? Like it's done by Microsoft when it comes to JSON https://learn.microsoft.com/en-us/dotnet/standard/serialization/system-text-json/customize-properties?pivots=dotnet-7-0



KK Karthikraja Kalaimani Syncfusion Team May 15, 2023 10:19 AM UTC

We have the ability to customize the header text for any property type bound to the ItemsSource property of the SfDataGrid during the AutoGeneratingColumn event. This event allows us to modify the column header text based on our specific requirements. By handling this event, we can dynamically set the header text for each column based on the mapping name being displayed in the grid.


Loader.
Up arrow icon