Articles in this section
Category / Section

How to create DataGridTemplateColumn from code-behind in MAUI DataGrid (SfDataGrid)?

2 mins read

The .NET MAUI DataGrid (SfDataGrid) allows you to load custom views in its DataGridCell using the DataGridTemplateColumn. It can be created both in XAML and in the code behind. You can load any view inside the DataGridCell by customizing the CellTemplate property of the DataGridTemplateColumn.

The below code illustrates how to add DataGridTemplateColumn in code-behind.

C#

public partial class MainPage : ContentPage
{
    private SfDataGrid sfDataGrid;
    private EmployeeViewModel gridData;
 
    public MainPage()
 {
  InitializeComponent();
        sfDataGrid = new SfDataGrid();
        gridData = new EmployeeViewModel();
        sfDataGrid.AutoGenerateColumnsMode = AutoGenerateColumnsMode.None;
        var templateColumn = new DataGridTemplateColumn()
        {
            MappingName = "EmployeeID",
            HeaderText = "Employee ID",
            Width = 100
        };
        var dataTemplate = new DataTemplate(() =>
        {
            var label = new Label()
            {
                TextColor = Colors.Blue,
                VerticalOptions = LayoutOptions.Center,
                HorizontalOptions = LayoutOptions.Center
            };
            label.SetBinding(Label.TextProperty, "EmployeeID");
            return label;
        });
        templateColumn.CellTemplate = dataTemplate;
        sfDataGrid.Columns.Add(templateColumn);
        sfDataGrid.Columns.Add(new DataGridTextColumn() { MappingName = "Name", Width = 100 });
        sfDataGrid.Columns.Add(new DataGridTextColumn() { MappingName = "ContactID",HeaderText="Contact ID", Width = 100 });
        sfDataGrid.ItemsSource = gridData.Employees;
        this.Content = sfDataGrid;
    } 
}                

how to create datagrid template column from code behind.

 

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 DataGridTemplateColumn from code behind 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