|
1 | | -# How-to-set-multiple-selection-background-in-.NET-MAUI-DataGrid |
2 | | -How to set multiple selection background in .NET MAUI DataGrid |
| 1 | +# How to set multiple selection background in .NET MAUI DataGrid |
| 2 | + |
| 3 | +In this article, we will show you how to set multiple selection background in [.NET MAUI DataGrid](https://www.syncfusion.com/maui-controls/maui-datagrid). |
| 4 | + |
| 5 | +**XAML** |
| 6 | +``` |
| 7 | + <syncfusion:SfDataGrid x:Name="sfGrid" |
| 8 | + ColumnWidthMode="Auto" |
| 9 | + GridLinesVisibility="Both" |
| 10 | + HeaderGridLinesVisibility="Both" |
| 11 | + AutoGenerateColumnsMode="None" |
| 12 | + SelectionMode="Multiple" |
| 13 | + ItemsSource="{Binding Employees}"> |
| 14 | + |
| 15 | + <syncfusion:SfDataGrid.Columns> |
| 16 | + <syncfusion:DataGridNumericColumn MappingName="EmployeeID" |
| 17 | + HeaderText="Employee ID" /> |
| 18 | + <syncfusion:DataGridTextColumn MappingName="Name" |
| 19 | + HeaderText="Employee Name" /> |
| 20 | + <syncfusion:DataGridTextColumn MappingName="Title" |
| 21 | + HeaderText="Designation" /> |
| 22 | + <syncfusion:DataGridDateColumn MappingName="HireDate" |
| 23 | + HeaderText="Hire Date" /> |
| 24 | + </syncfusion:SfDataGrid.Columns> |
| 25 | + </syncfusion:SfDataGrid> |
| 26 | + ``` |
| 27 | + |
| 28 | +C# |
| 29 | +The below code illustrates how to set multiple selection background with custom Cell renderer. |
| 30 | +``` |
| 31 | + public partial class MainPage : ContentPage |
| 32 | + { |
| 33 | + public MainPage() |
| 34 | + { |
| 35 | + InitializeComponent(); |
| 36 | + sfGrid.CellRenderers.Remove("Text"); |
| 37 | + sfGrid.CellRenderers.Add("Text", new CustomRenderer()); |
| 38 | + sfGrid.CellRenderers.Remove("Numeric"); |
| 39 | + sfGrid.CellRenderers.Add("Numeric", new CustomRenderer()); |
| 40 | + sfGrid.CellRenderers.Remove("CheckBox"); |
| 41 | + sfGrid.CellRenderers.Add("CheckBox", new CustomRenderer()); |
| 42 | + sfGrid.CellRenderers.Remove("Template"); |
| 43 | + sfGrid.CellRenderers.Add("Template", new CustomRenderer()); |
| 44 | + sfGrid.CellRenderers.Remove("Image"); |
| 45 | + sfGrid.CellRenderers.Add("Image", new CustomRenderer()); |
| 46 | + sfGrid.CellRenderers.Remove("DateTime"); |
| 47 | + sfGrid.CellRenderers.Add("DateTime", new CustomRenderer()); |
| 48 | + sfGrid.CellRenderers.Remove("ComboBox"); |
| 49 | + sfGrid.CellRenderers.Add("ComboBox", new CustomRenderer()); |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + public class CustomRenderer : DataGridCellRenderer<View,View> |
| 54 | + { |
| 55 | + protected override void OnSetCellStyle(DataColumnBase dataColumn) |
| 56 | + { |
| 57 | + base.OnSetCellStyle(dataColumn); |
| 58 | + |
| 59 | + if (dataColumn != null) |
| 60 | + { |
| 61 | + |
| 62 | + var gridStyle = this.DataGrid?.DefaultStyle; |
| 63 | + DataGridCell? gridCell = dataColumn.ColumnElement; |
| 64 | + var dataRow = dataColumn.GetType().GetRuntimeProperties().FirstOrDefault(x => x.Name.Equals("DataRow"))!.GetValue(dataColumn); |
| 65 | + |
| 66 | + if (DataGrid!.SelectionController.SelectedRows.Any(row => row.RowData == dataColumn.RowData && (dataRow as DataRow)!.IsSelectedRow)) |
| 67 | + { |
| 68 | + var rowData = (dataRow as DataRow).RowData as Employee; |
| 69 | + if(rowData.Title.Equals("Assistant")) |
| 70 | + (gridCell as DataGridCell).Background = Color.FromRgba("caf0f8"); |
| 71 | + else if (rowData.Title.Equals("Engineering")) |
| 72 | + (gridCell as DataGridCell).Background = Color.FromRgba("00b4d8"); |
| 73 | + else if (rowData.Title.Equals("Designer")) |
| 74 | + (gridCell as DataGridCell).Background = Color.FromRgba("ff99c8"); |
| 75 | + else if (rowData.Title.Equals("Manager")) |
| 76 | + (gridCell as DataGridCell).Background = Color.FromRgba("fb6f92"); |
| 77 | + } |
| 78 | + |
| 79 | + gridStyle = null; |
| 80 | + gridCell = null; |
| 81 | + } |
| 82 | + } |
| 83 | + } |
| 84 | +``` |
| 85 | + |
| 86 | + |
| 87 | +  |
| 88 | + |
| 89 | +[View sample in GitHub](https://github.com/SyncfusionExamples/How-to-set-multiple-selection-background-in-.NET-MAUI-DataGrid) |
| 90 | + |
| 91 | +Take a moment to explore this [documentation](https://help.syncfusion.com/maui/datagrid/overview), where you can find more information about Syncfusion .NET MAUI DataGrid (SfDataGrid) with code examples. Please refer to this [link](https://www.syncfusion.com/maui-controls/maui-datagrid) to learn about the essential features of Syncfusion .NET MAUI DataGrid (SfDataGrid). |
| 92 | + |
| 93 | +##### Conclusion |
| 94 | + |
| 95 | +I hope you enjoyed learning about how to set multiple selection background in .NET MAUI DataGrid. |
| 96 | + |
| 97 | +You can refer to our [.NET MAUI DataGrid’s feature tour](https://www.syncfusion.com/maui-controls/maui-datagrid) page to learn about its other groundbreaking feature representations. You can also explore our [.NET MAUI DataGrid Documentation](https://help.syncfusion.com/maui/datagrid/getting-started) to understand how to present and manipulate data. |
| 98 | +For current customers, you can check out our .NET MAUI components on the [License and Downloads](https://www.syncfusion.com/sales/teamlicense) page. If you are new to Syncfusion, you can try our 30-day [free trial](https://www.syncfusion.com/downloads/maui) to explore our .NET MAUI DataGrid and other .NET MAUI components. |
| 99 | + |
| 100 | +If you have any queries or require clarifications, please let us know in the comments below. You can also contact us through our [support forums](https://www.syncfusion.com/forums), [Direct-Trac](https://support.syncfusion.com/create) or [feedback portal](https://www.syncfusion.com/feedback/maui?control=sfdatagrid), or the feedback portal. We are always happy to assist you! |
0 commit comments