BoldSignA modern eSignature application with affordable pricing. Sign up today for unlimited document usage!
//Main Page
Style style = new Style(typeof(GridCell));
style.Setters.Add(new Setter() { Property = GridCell.BackgroundColorProperty, Value = newBinding() { Path = "OrderID", Converter = new Converter() } });
foreach (var column in sfGrid.Columns)
{
if (column.MappingName == "OrderID")
column.CellStyle = style;
}
//Converter class
public class Converter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
var data = (int)value;
if (data < 0)//You can change the condition based on your requirement
return Color.Red;
else
return Color.Green;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
return null;
}
} |
//Initialize the style.
style = new Style(typeof(GridCell));
style.Setters.Add(new Setter() { Property = GridCell.BackgroundColorProperty,Value = new Binding() { Path = "OrderID",Converter = new Converter(), ConverterParameter=sfGrid } }); //Need to pass SfDataGrid as ConverterParameter. So that you can access the grid in converter.
//Set the style for the column in GridViewCreated event.
private void SfGrid_GridViewCreated(object sender, GridViewCreatedEventArgs e)
{
foreach (var column in sfGrid.Columns)
{
if (column.MappingName == "OrderID")
{
column.CellStyle = style;
}
}
}
//Converter class.
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
//You can get the grid here since we passed SfDataGrid as parameter.
var grid = parameter as SfDataGrid;
var converterValue = (int)value;
List<int> list = new List<int>();
foreach (var item in grid.View.Records)
{
foreach (var column in grid.Columns)
{
//You can apply this for all the columns as below.
if (column.MappingName == "OrderID")
{
var cellValue = (int)grid.GetCellValue(item.Data, column.MappingName);
list.Add(cellValue);
}
}
}
//Max and Min values will be stored here.
var maxValue = list.ToArray().Max();
var minValue = list.ToArray().Min();
//Background will be set based on the condition
if (converterValue == maxValue)
{
return Color.Green;
}
else if (converterValue == minValue)
{
return Color.Red;
}
else
return Color.Default;
} |