Articles in this section
Category / Section

How to Set Header Style in Code Behind .NET MAUI DataGrid?

3 mins read

In this article, we will show you how to set the HeaderStyle in code-behind in .NET MAUI DataGrid.

Xaml.cs
The code below demonstrates how to set the HeaderStyle in code-behind in SfDataGrid.

public partial class MainPage : ContentPage
{
    public SfDataGrid sfGrid;

    public MainPage()
    {
        InitializeComponent();

        Title = "Employees List";
        EmployeeViewModel viewModel = new EmployeeViewModel();
        sfGrid = new SfDataGrid
        {
            GridLinesVisibility = GridLinesVisibility.Both,
            HeaderGridLinesVisibility = GridLinesVisibility.Both,
            ColumnWidthMode = ColumnWidthMode.Auto,
            AutoGenerateColumnsMode = AutoGenerateColumnsMode.None,
            ItemsSource = viewModel.Employees,
        };

        // Create a new style for the header
        var headerStyle = new Style(typeof(DataGridRowHeaderCell))
        {
            Setters =
            {
                new Setter { Property = DataGridRowHeaderCell.TextColorProperty, Value = Colors.Yellow },
                new Setter { Property = DataGridRowHeaderCell.FontAttributesProperty, Value = FontAttributes.Bold },
                new Setter { Property = DataGridRowHeaderCell.FontSizeProperty, Value = 25.0 },
            }
        };

        sfGrid.Columns.Add(new DataGridNumericColumn
        {
            MappingName = "EmployeeID",
            Format = "#",
            HeaderText = "Employee ID",
            HeaderStyle = headerStyle,

        });

        sfGrid.Columns.Add(new DataGridTextColumn
        {
            MappingName = "Name",
            HeaderText = "Employee Name"
        });

        sfGrid.Columns.Add(new DataGridTextColumn
        {
            MappingName = "Title",
            HeaderText = "Designation"
        });

        sfGrid.Columns.Add(new DataGridDateColumn
        {
            MappingName = "HireDate",
            HeaderText = "Hire Date"
        });

        this.Content = sfGrid;
    }
}

Output

Download the complete sample from GitHub.

Conclusion

I hope you enjoyed learning how to set header style in code behind .NET MAUI DataGrid.

You can refer to our .NET MAUI DataGrid feature tour page to know about its other groundbreaking feature representations and Documentation, and how to quickly get started with configuration specifications. You can also explore our .NET MAUI DataGrid example to understand how to create and manipulate data.

For current customers, check out our components from the License and Downloads page. If you are new to Syncfusion®, try our 30-day free trial to check out our other controls.
Please let us know in the comments section if you have any queries or require clarification. You can also contact us through our support forums, Direct-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