Colour Cells based on Decimal Value Ranges C# (2 methods)

Tips submitted by ASP.NET Maker users
Post Reply
xgis
User
Posts: 68

Colour Cells based on Decimal Value Ranges C# (2 methods)

Post by xgis »

// The following code is designed to colour cells in increasing severity using C# comparison operators and float values from a database.
// Float values are compared as decimals firstly by converting them to decimal and then using the > and <= operators to define the number range.
// The & operator joins the ranges for comparison. The M added to the end of the value is the money symbol
// In C# 'M' means decimal (eg 0.0M). If you don't add it the number will be treated as a double. D is double.
// This code solution was written to resolve the error Operator '' cannot be applied to operands of type 'decimal' and 'double'
// It is always important to check for 'null' values to prevent errors.
// The else statement at the end is empty but for testing can be set to a colour like black to indicate that none of the conditions have been met.
// In this example numbers are not entered manually. They are calculated within defined ranges using javascript client side code to populate C# readonly fields.
// The following codes may be integrated into 'Rendered' or 'Updated' and similar C# events (eg Common Row_Rendered, Row-Updated)
// A second method is also shown for altering the start points of the range

// Method 1 (Decimal Ranges) - This implementation also changes the range end point to be <=2 (end of range) eg 2.00
// This can be adjusted by making the left hand side range '>' to represent the start of the range and '<=' for end of range

if (SafetyRiskValue.CurrentValue is System.DBNull)
{
SafetyRiskValue.CellCssStyle = "background-color: #C6E2FF";
SafetyRating.CellCssStyle = "background-color: #C6E2FF";
}
else if ((Convert.ToDecimal(SafetyRiskValue.CurrentValue) >= 0.0M)& (Convert.ToDecimal(SafetyRiskValue.CurrentValue) <= 2.0M))
{
SafetyRiskValue.CellCssStyle = "background-color: #FFD966";
SafetyRating.CellCssStyle = "background-color: #FFD966";
}
else if ((Convert.ToDecimal(SafetyRiskValue.CurrentValue) > 2.0M)& (Convert.ToDecimal(SafetyRiskValue.CurrentValue) <= 4.0M))
{
SafetyRiskValue.CellCssStyle = "background-color: #F9C475";
SafetyRating.CellCssStyle = "background-color: #F9C475";
}
else if ((Convert.ToDecimal(SafetyRiskValue.CurrentValue) > 4.0M)& (Convert.ToDecimal(SafetyRiskValue.CurrentValue) <= 6.0M))
{
SafetyRiskValue.CellCssStyle = "background-color: #F4B084";
SafetyRating.CellCssStyle = "background-color: #F4B084";
}
else if ((Convert.ToDecimal(SafetyRiskValue.CurrentValue) > 6.0M)& (Convert.ToDecimal(SafetyRiskValue.CurrentValue) <= 8.0M))
{
SafetyRiskValue.CellCssStyle = "background-color: #F68C77";
SafetyRating.CellCssStyle = "background-color: #F68C77";
}
else if ((Convert.ToDecimal(SafetyRiskValue.CurrentValue) > 8.0M)& (Convert.ToDecimal(SafetyRiskValue.CurrentValue) <= 10.0M))
{
SafetyRiskValue.CellCssStyle = "background-color: #F8696B";
SafetyRating.CellCssStyle = "background-color: #F8696B";
}
else { }

// Method 2 (Decimal Ranges) - This implementation also changes the range end point to be <2 (end of range) eg 1.99
// This can be adjusted by making the left hand side range '>=' to represent the start of the range and '<' for end of range (except the last value)

if (SafetyRiskValue.CurrentValue is System.DBNull)
{
SafetyRiskValue.CellCssStyle = "background-color: #C6E2FF";
SafetyRating.CellCssStyle = "background-color: #C6E2FF";
}
else if ((Convert.ToDecimal(SafetyRiskValue.CurrentValue) >= 0.0M)& (Convert.ToDecimal(SafetyRiskValue.CurrentValue) < 2.0M))
{
SafetyRiskValue.CellCssStyle = "background-color: #FFD966";
SafetyRating.CellCssStyle = "background-color: #FFD966";
}
else if ((Convert.ToDecimal(SafetyRiskValue.CurrentValue) >= 2.0M)& (Convert.ToDecimal(SafetyRiskValue.CurrentValue) < 4.0M))
{
SafetyRiskValue.CellCssStyle = "background-color: #F9C475";
SafetyRating.CellCssStyle = "background-color: #F9C475";
}
else if ((Convert.ToDecimal(SafetyRiskValue.CurrentValue) >= 4.0M)& (Convert.ToDecimal(SafetyRiskValue.CurrentValue) < 6.0M))
{
SafetyRiskValue.CellCssStyle = "background-color: #F4B084";
SafetyRating.CellCssStyle = "background-color: #F4B084";
}
else if ((Convert.ToDecimal(SafetyRiskValue.CurrentValue) >= 6.0M)& (Convert.ToDecimal(SafetyRiskValue.CurrentValue) < 8.0M))
{
SafetyRiskValue.CellCssStyle = "background-color: #F68C77";
SafetyRating.CellCssStyle = "background-color: #F68C77";
}
else if ((Convert.ToDecimal(SafetyRiskValue.CurrentValue) >= 8.0M)& (Convert.ToDecimal(SafetyRiskValue.CurrentValue) <= 10.0M))
{
SafetyRiskValue.CellCssStyle = "background-color: #F8696B";
SafetyRating.CellCssStyle = "background-color: #F8696B";
}
else { }


Post Reply