Articles in this section
Category / Section

How to create a custom DataGridColumn in MAUI DataGrid (SfDataGrid)?

2 mins read

The .NET MAUI DataGrid (SfDataGrid) allows you to create custom DataGridColumn by following the below steps.

Step 1 - To create a custom column, you must inherit from the DataGridColumn class and specify the DataGridColumn.CellType property.

Step 2 - A custom CellRenderer  class must be created for the custom column by customizing the DataGridCellRenderer<D, E>.

Step 3 - In the created cell renderer class, you should override the following methods.

OnCreateDisplayUIView() – Generate the data to be placed in the grid cell.

OnInitializeDisplayView() – To set up the cell content loaded in the grid cell with the necessary settings.

Step 4 - You must add the custom column to the SfDataGrid.Columns collection and its renderer to the SfDataGrid.CellRenderers collection.

For example, refer the below example in which a custom column is created for loading a Stepper control in the grid columns. A custom renderer class is written to load Stepper control as display.

XAML

<syncfusion:SfDataGrid x:Name="dataGrid"  
                           ItemsSource="{Binding Employees}"       
                           ColumnWidthMode="Auto"
                           AutoGenerateColumnsMode="None">
        <syncfusion:SfDataGrid.Columns>
            <local:StepperColumn MappingName="OrderID"/>
        </syncfusion:SfDataGrid.Columns>
  </syncfusion:SfDataGrid>

 

Creating Stepper Column

C#

 
public class StepperColumn : DataGridColumn
    {
        public StepperColumn()
        {
            var cellType = typeof(DataGridColumn).GetRuntimeProperties().FirstOrDefault((property) => property.Name == "CellType");
            cellType.SetValue(this, "Stepper");
        }
    }

Custom cell renderer for StepperColumn

 
public class StepperColumnRenderer : DataGridCellRenderer<Stepper, Stepper>
{
    public StepperColumnRenderer()
    {
    }
 
    protected override Stepper OnCreateDisplayUIView()
    {
        return new Stepper();
    }
}

MainPage

public partial class MainPage : ContentPage
{
    
    public MainPage()
    {
        InitializeComponent();
        dataGrid.CellRenderers.Add("Stepper", new StepperColumnRenderer());        
    } 
}

how to add custom datagrid column

View Sample on GitHub

Take a moment to pursue this documentation, where you can find more about Syncfusion .NET MAUI DataGrid (SfDataGrid) with code examples.

Please refer to this link to learn about the essential features of Syncfusion .NET MAUI DataGrid (SfDataGrid).

Conclusion

I hope you enjoyed learning about how to create a custom DataGridColumn in MAUI DataGrid (SfDataGrid).

You can refer to our .NET MAUI DataGrid’s feature tour page to know about its other groundbreaking feature representations. You can also explore our .NET MAUI DataGrid Documentation to understand how to present and manipulate data.

For current customers, you can check out our .NET MAUI components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our .NET MAUI DataGrid and other .NET MAUI components.

If you have any queries or require clarifications, please let us know in comments below. You can also contact us through our support forumsDirect-Trac or feedback portal. We are always happy to assist you!

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please  to leave a comment
Access denied
Access denied