Skip to content

SyncfusionExamples/how-to-create-gridtemplatecolumn-from-code-behind-in-maui-datagrid

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

How to create DataGridTemplate column from code behind in MAUI DataGrid

In.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 DataGridTemplateColumn

Conclusion

I hope you enjoyed learning about how to you 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 forums, Direct-Trac or feedback portal. We are always happy to assist you!

About

how to create gridtemplatecolumn from code-behind in maui datagrid

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 5

Languages