Hi, how can I set a strike through font for a row?
all columns are added manually with Columns.Add() and I would like to strikethrough the entire row based on one cell on that row.
Hi Phunction,
You can fulfill your requirement by overriding the `SetCellStyle` method
of the corresponding column renderer. In this method, you can set the “SfDataGridLabel.TextDecorations”
property to “TextDecorations.Strikethrough” based on the condition. We
have provided a sample for your reference, which demonstrates this approach.
Please review the attached sample and the following code snippet for further
details.
|
using Syncfusion.Maui.Core.Internals; using Syncfusion.Maui.DataGrid; using System.Globalization;
namespace MauiApp1 { public partial class MainPage : ContentPage { public MainPage() { InitializeComponent();
dataGrid.CellRenderers.Remove("Text"); dataGrid.CellRenderers.Add("Text", new CustomDataGridTextRenderer());
ColumnCollection columns = new ColumnCollection();
columns.Add(new DataGridTextColumn() { MappingName = "OrderID", HeaderText = "Order ID" }); columns.Add(new DataGridTextColumn() { MappingName = "CustomerID", HeaderText = "Customer ID" }); columns.Add(new DataGridTextColumn() { MappingName = "Customer", HeaderText = "Customer Name" }); columns.Add(new DataGridTextColumn() { MappingName = "ShipCity", HeaderText = "Ship City" }); columns.Add(new DataGridTextColumn() { MappingName = "ShipCountry", HeaderText = "Ship Country" }); columns.Add(new DataGridTextColumn() { MappingName = "Price", HeaderText = "Price" });
dataGrid.Columns = columns; } }
public class CustomDataGridTextRenderer : DataGridTextBoxCellRenderer { protected override void OnSetCellStyle(DataColumnBase dataColumn) { base.OnSetCellStyle(dataColumn);
var data = dataColumn.RowData as OrderInfo;
if (data != null && data.OrderID > 1002 && data.OrderID < 1009) { if (dataColumn.ColumnElement != null && dataColumn.ColumnElement.Content is SfDataGridLabel label) { label.TextDecorations = TextDecorations.Strikethrough;
} } else { if (dataColumn.ColumnElement != null && dataColumn.ColumnElement.Content is SfDataGridLabel label) { label.TextDecorations = TextDecorations.None; } } }
} }
|
Regards,
Tamilarasan
Thanks, will this also work for other column types like combobox, numeric?
Hi Phunction,
The provided code snippet will only work correctly for text columns. If you wish to tailor this functionality for other column types, you'll need to override the renderer for the specific column you intend to customize. For your reference, we've included a sample and a code snippet demonstrating how to customize the style for text and numeric column.
Code snippet:
|
public partial class MainPage : ContentPage { public MainPage() { InitializeComponent(); dataGrid.CellRenderers.Remove("Text"); dataGrid.CellRenderers.Remove("Numeric"); dataGrid.CellRenderers.Add("Text", new CustomDataGridTextRenderer()); dataGrid.CellRenderers.Add("Numeric", new CustomNumericRenderer()); } }
public class CustomDataGridTextRenderer : DataGridTextBoxCellRenderer { protected override void OnSetCellStyle(DataColumnBase dataColumn) { base.OnSetCellStyle(dataColumn);
var data = dataColumn.RowData as Employee;
if (data != null && data.Name.Equals("Sean Jacobson")) { if (dataColumn.ColumnElement != null && dataColumn.ColumnElement.Content is SfDataGridLabel label) { label.TextDecorations = TextDecorations.Strikethrough; } } else { if (dataColumn.ColumnElement != null && dataColumn.ColumnElement.Content is SfDataGridLabel label) { label.TextDecorations = TextDecorations.None; } } }
}
public class CustomNumericRenderer : DataGridNumericCellRenderer { protected override void OnSetCellStyle(DataColumnBase dataColumn) { base.OnSetCellStyle(dataColumn);
var data = dataColumn.RowData as Employee;
if (data != null && data.EmployeeID > 1002 && data.EmployeeID < 1009) { if (dataColumn.ColumnElement != null && dataColumn.ColumnElement.Content is SfDataGridLabel label) { label.TextDecorations = TextDecorations.Strikethrough; } } else { if (dataColumn.ColumnElement != null && dataColumn.ColumnElement.Content is SfDataGridLabel label) { label.TextDecorations = TextDecorations.None; } } }
}
|
Regards,
Nirmalkumar
What is the one for the combo box, is it:
dataGrid.CellRenderers.Remove("Combobox");
Hi Phunction,
We have attached the code snippet for your reference. If you have any further concerns, please feel free to reach out to us.
Code snippet:
dataGrid.CellRenderers.Remove("ComboBox");
Regards,
Sudarsan Muthuselvan
This does work for what I am doing, but I do get this error for MAC:
Error CS1061 'SfDataGridLabel' does not contain a definition for 'TextDecorations' and no accessible extension method 'TextDecorations' accepting a first argument of type 'SfDataGridLabel' could be found (net8.0-maccatalyst)
I am ignoring it as I am not building for MAC, but maybe it should be fixed in a future version?
Hi Phunction,
We have considered the issue regarding the MAC platform. Currently, you can
limit the display of the error message by setting the `Label.TextDecorations` property for all platforms except the MAC platform.
Please refer to the following code snippet for detailed instructions.
|
public class CustomDataGridTextRenderer : DataGridTextBoxCellRenderer { protected override void OnSetCellStyle(DataColumnBase dataColumn) { base.OnSetCellStyle(dataColumn);
var data = dataColumn.RowData as OrderInfo;
if (data != null && data.OrderID > 1002 && data.OrderID < 1009) {
if (dataColumn.ColumnElement != null && dataColumn.ColumnElement.Content is SfDataGridLabel label) {
#if !MACCATALYST // This will set for all the platforms except Mac Catalyst label.TextDecorations = TextDecorations.Strikethrough; #endif } } else { if (dataColumn.ColumnElement != null && dataColumn.ColumnElement.Content is SfDataGridLabel label) {
#if !MACCATALYST
label.TextDecorations = TextDecorations.None; #endif
} } } } |
Regards,
Tamilarasan