BoldSignEasily embed eSignatures in your .NET applications. Free sandbox with native SDK available.
Hi,I'm working with the SfDataGrid and now I have to apply some conditional styles: https://help.syncfusion.com/xamarin/sfdatagrid/conditional-stylesI've been reading the documentation but I can't find how to do what I need:
- Define row style (for example bold font for all the cells) depending on a determined property of the model (also I want to hide the column corresponding to this property).
For example you can think about having a model property "bold" which, if true, the cells of this row have to be in bold font. Otherwise not.
And of course this property has not to be a column in the grid, only to determine the style of its cells.- Define cell style depending on the value. This should be different for each column.
For example in the row "growth", if the value is lower than 1, it should be printed in red, otherwise in green.
Otherwise, the row "sales" has to be printed red if lower than 0 (instead of 1 as growth).
And so on for the rows..- Define the type of the cells (integer, double, percentage(?), etc) and the precision (number of decimals).
The last thing I have to point out is that I'm working in a programatically way, so I do not have any XML/XAML to define the grid/columns structure.Is it posible this way? Or should I use the XML/XAML way?Thanks a lot,Joan
//Add customrenderer
dataGrid.CellRenderers.Remove("TextView");
dataGrid.CellRenderers.Add("TextView", new GridCellTextViewRendererExt());
public class GridCellTextViewRendererExt : GridCellTextViewRenderer
{
public GridCellTextViewRendererExt()
{
}
public override void OnInitializeDisplayView(DataColumnBase dataColumn, SfLabel view)
{
base.OnInitializeDisplayView(dataColumn, view);
if ((dataColumn.RowData as OrderInfo).IsBold == true)
{
view.FontAttributes = FontAttributes.Bold;
}
}
} |
GridTextColumn orderIDColumn = new GridTextColumn();
orderIDColumn.MappingName = "OrderID";
Style style = new Style(typeof(GridCell));
style.Setters.Add(new Setter() { Property = GridCell.ForegroundProperty, Value = new Binding("OrderID", BindingMode.TwoWay, new StyleConverter()) });
orderIDColumn.CellStyle = style;
orderIDColumn.LoadUIView = true;
dataGrid.Columns.Add(orderIDColumn); |
public class StyleConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
int _value = (int)value;
if (_value % 2 == 0)
return Color.Green;
return Color.Red;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return value;
}
} |
SfDataGrid.HeaderRowHeight = 0; |
Hi Joan,
Thanks for contacting Syncfusion support.
Query regarding TextSize as Bold:
You can achieve your requirement by customizing GridCellTextViewRenderer. You have to set the GridColumn.LoadUIView as True while customize the GridCellTextViewRenderer. Please refer the below code example for GridCellTextViewRenderer customization
//Add customrendererdataGrid.CellRenderers.Remove("TextView");dataGrid.CellRenderers.Add("TextView", new GridCellTextViewRendererExt());public class GridCellTextViewRendererExt : GridCellTextViewRenderer{public GridCellTextViewRendererExt(){}public override void OnInitializeDisplayView(DataColumnBase dataColumn, SfLabel view){base.OnInitializeDisplayView(dataColumn, view);if ((dataColumn.RowData as OrderInfo).IsBold == true){view.FontAttributes = FontAttributes.Bold;}}}
Query regarding Custom style:
You can achieve your requirement by setting Style to GridColumn.CellStyle as in the below code example
GridTextColumn orderIDColumn = new GridTextColumn();orderIDColumn.MappingName = "OrderID";Style style = new Style(typeof(GridCell));style.Setters.Add(new Setter() { Property = GridCell.ForegroundProperty, Value = new Binding("OrderID", BindingMode.TwoWay, new StyleConverter()) });orderIDColumn.CellStyle = style;orderIDColumn.LoadUIView = true;dataGrid.Columns.Add(orderIDColumn);
Refer the below code example for writing a converter to customize the cell foreground based on conditions
public class StyleConverter : IValueConverter{public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture){int _value = (int)value;if (_value % 2 == 0)return Color.Green;return Color.Red;}public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture){return value;}}
Regards,Ashok