Articles in this section
Category / Section

How to apply SelectedRowTextColor to the cells of a DataGridTemplateColumn in MAUI DataGrid (SfDataGrid)?

5 mins read

The current implementation of the MAUI SfDataGrid, the SelectedRowTextColor would not apply to the cells of DataGridTemplateColumn. This article illustrates how to apply SelectedRowTextColor for DataGridTemplateColumn cells in a .NET MAUI DataGrid.

  • Initialize the SfDataGrid with the required properties, such as ItemsSource, SelectedRowTextColor in the SfDataGrid.DefaultStyle, and use DataGridTemplateColumn for the required columns.

  • Write a custom renderer class that extends from the DataGridCellTemplateRenderer class. In the overridden OnSetCellStyle method, check if the respective row is present in DataGrid.SelectedRows. If it is, set the SelectedRowTextColor for the content of the DataGridTemplateColumn cell; otherwise, set the RowTextColor to the text color of the content.

  • To register the custom cell template renderer with the SfDataGrid.CellRenderers collection, remove the existing entry by its key and then add a new entry with the same key, along with an instance of the custom renderer.

XAML

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
           xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
           xmlns:local="clr-namespace:MauiApp1"
           xmlns:dataGrid="clr-namespace:Syncfusion.Maui.DataGrid;assembly=Syncfusion.Maui.DataGrid"
           x:Class="MauiApp1.MainPage">
  <ContentPage.BindingContext>
      <local:OrderInfoRepository x:Name="viewModel"/>
  </ContentPage.BindingContext>

  <dataGrid:SfDataGrid x:Name="dataGrid"
                       ItemsSource="{Binding OrderInfoCollection}" 
                       SelectionMode="Single"
                       ColumnWidthMode="Auto">
      <dataGrid:SfDataGrid.DefaultStyle>
          <dataGrid:DataGridStyle SelectedRowTextColor="Red">
          </dataGrid:DataGridStyle>
      </dataGrid:SfDataGrid.DefaultStyle>
      <dataGrid:SfDataGrid.Columns>
          <dataGrid:DataGridTemplateColumn HeaderText="Order ID" MappingName="OrderID">
              <dataGrid:DataGridTemplateColumn.CellTemplate>
                  <DataTemplate>
                      <Label Text="{Binding OrderID}"/>
                  </DataTemplate>
              </dataGrid:DataGridTemplateColumn.CellTemplate>
          </dataGrid:DataGridTemplateColumn>
      </dataGrid:SfDataGrid.Columns>
  </dataGrid:SfDataGrid>
</ContentPage>

MainPage.xaml.cs

namespace MauiApp1
{

   public partial class MainPage : ContentPage
   {

       public MainPage()
       {
           InitializeComponent();
           dataGrid.CellRenderers.Remove("Template");
           dataGrid.CellRenderers.Add("Template", new CustomDataGridCellTemplateRenderer());

       }
   }

   public class CustomDataGridCellTemplateRenderer : DataGridCellTemplateRenderer
   {
       protected override void OnSetCellStyle(DataColumnBase dataColumn)
       {
           base.OnSetCellStyle(dataColumn);

           if (dataColumn?.DataGridColumn != null)
           {
               var gridStyle = this.DataGrid?.DefaultStyle;
               DataGridCell gridCell = dataColumn.ColumnElement as DataGridCell;

               if (this.DataGrid.SelectedRows.Any(row => row == dataColumn.RowData))
               {
                   gridCell.TextColor = gridStyle?.SelectedRowTextColor;
                   Label sfLabel = dataColumn.ColumnElement?.Content as Label;
                   if (sfLabel != null)
                   {
                       sfLabel.TextColor = gridStyle?.SelectedRowTextColor;
                   }
               }
               else
               {
                   gridCell.TextColor = gridStyle?.RowTextColor;
                   Label sfLabel = dataColumn.ColumnElement?.Content as Label;
                   if (sfLabel != null)
                   {
                       sfLabel.TextColor = gridStyle?.RowTextColor;
                   }
               }
           }
       }
   }
}

Output
image.png

Download the complete sample from GitHub

Conclusion
I hope you enjoyed learning how to apply SelectedRowTextColor to the cells of a DataGridTemplateColumn in 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. 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