How to set multiple selection background in .NET MAUI DataGrid?
In this article, we will demonstrate how to set a custom background for multiple selected rows in the .NET MAUI DataGrid.
XAML
<syncfusion:SfDataGrid x:Name="sfGrid"
ColumnWidthMode="Auto"
GridLinesVisibility="Both"
HeaderGridLinesVisibility="Both"
AutoGenerateColumnsMode="None"
SelectionMode="Multiple"
ItemsSource="{Binding Employees}">
<syncfusion:SfDataGrid.Columns>
<syncfusion:DataGridNumericColumn MappingName="EmployeeID"
HeaderText="Employee ID" />
<syncfusion:DataGridTextColumn MappingName="Name"
HeaderText="Employee Name" />
<syncfusion:DataGridTextColumn MappingName="Title"
HeaderText="Designation" />
<syncfusion:DataGridDateColumn MappingName="HireDate"
HeaderText="Hire Date" />
</syncfusion:SfDataGrid.Columns>
</syncfusion:SfDataGrid>
C#
The code below illustrates how to set multiple selection background with a custom DataGridCellRenderer.
public partial class MainPage : ContentPage
{
public MainPage()
{
InitializeComponent();
sfGrid.CellRenderers.Remove("Text");
sfGrid.CellRenderers.Add("Text", new CustomRenderer());
sfGrid.CellRenderers.Remove("Numeric");
sfGrid.CellRenderers.Add("Numeric", new CustomRenderer());
sfGrid.CellRenderers.Remove("CheckBox");
sfGrid.CellRenderers.Add("CheckBox", new CustomRenderer());
sfGrid.CellRenderers.Remove("Template");
sfGrid.CellRenderers.Add("Template", new CustomRenderer());
sfGrid.CellRenderers.Remove("Image");
sfGrid.CellRenderers.Add("Image", new CustomRenderer());
sfGrid.CellRenderers.Remove("DateTime");
sfGrid.CellRenderers.Add("DateTime", new CustomRenderer());
sfGrid.CellRenderers.Remove("ComboBox");
sfGrid.CellRenderers.Add("ComboBox", new CustomRenderer());
}
}
public class CustomRenderer : DataGridCellRenderer<View,View>
{
protected override void OnSetCellStyle(DataColumnBase dataColumn)
{
base.OnSetCellStyle(dataColumn);
if (dataColumn != null)
{
var gridStyle = this.DataGrid?.DefaultStyle;
DataGridCell? gridCell = dataColumn.ColumnElement;
var dataRow = dataColumn.GetType().GetRuntimeProperties().FirstOrDefault(x => x.Name.Equals("DataRow"))!.GetValue(dataColumn);
if (DataGrid!.SelectionController.SelectedRows.Any(row => row.RowData == dataColumn.RowData && (dataRow as DataRow)!.IsSelectedRow))
{
var rowData = (dataRow as DataRow).RowData as Employee;
if(rowData.Title.Equals("Assistant"))
(gridCell as DataGridCell).Background = Color.FromRgba("caf0f8");
else if (rowData.Title.Equals("Engineering"))
(gridCell as DataGridCell).Background = Color.FromRgba("00b4d8");
else if (rowData.Title.Equals("Designer"))
(gridCell as DataGridCell).Background = Color.FromRgba("ff99c8");
else if (rowData.Title.Equals("Manager"))
(gridCell as DataGridCell).Background = Color.FromRgba("fb6f92");
}
gridStyle = null;
gridCell = null;
}
}
}
Output
Download the complete sample from GitHub
Conclusion
I hope you enjoyed learning how to set a multiple selection background in .NET MAUI DataGrid.
You can refer to our .NET MAUI DataGrid feature tour page to learn about its other groundbreaking feature representations. Explore our .NET MAUI DataGrid Documentation to understand how to present and manipulate data.
For current customers, check out our .NET MAUI components on the License and Downloads page. If you are new to Syncfusion®, try our 30-day free trial to explore our .NET MAUI DataGrid and other .NET MAUI components.
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, or the feedback portal. We are always happy to assist you!