Search results

Conditional Formatting

Conditional formatting allows users to change the appearance of pivot grid value cells with its background color, font color, font family, and font size based on specific conditions. To achieve this, set the allowConditionalFormatting to true and use the conditionalFormatSettings object in the pivot grid widget along with the following properties.

  • measure: Specifies the value field name for which style will be applied.
  • condition: Specifies the operator type such as equals, greater than, less than, etc.
  • value1: Specifies the start value.
  • value2: Specifies the end value.
  • style: Specifies the style for the cell.
razor
Formatting.cs
@using Syncfusion.EJ2.PivotView

@Html.EJS().PivotView("PivotGrid").Width("100%").Height("300").DataSource(dataSource => dataSource.Data((IEnumerable<object>)ViewBag.Data).ExpandAll(false).EnableSorting(true)
           .DrilledMembers(drilledmembers =>
           {
               drilledmembers.Name("Country").Items(ViewBag.drilledMembers).Add();
           })
           .Rows(rows =>
           {
               rows.Name("Country").Add();
               rows.Name("Products").Add();
           })
           .Columns(columns =>
           {
               columns.Name("Year").Add();
               columns.Name("Order_Source").Caption("Order Source").Add();
           })
           .Values(values =>
           {
               values.Name("In_Stock").Caption("In Stock").Add();
               values.Name("Sold").Caption("Units Sold").Add();
           })
           .Filters(filters =>
           {
               filters.Name("Product_Categories").Caption("Product Categories").Add();
           })
           .ConditionalFormatSettings( format =>
           {
               format.Conditions(Condition.LessThan).Measure("In Stock").Value1(5000).Style( style => { style.BackgroundColor("#80cbc4").Color("black").FontFamily("Tahoma").FontSize("12px"); }).Add();
               format.Conditions(Condition.Between).Measure("Units Sold").Value1(3400).Value2(40000).Style(style => { style.BackgroundColor("#f48fb1").Color("black").FontFamily("Tahoma").FontSize("12px"); }).Add();
           })
           ).AllowConditionalFormatting(true).Render()
public ActionResult Index()
{
    var data = GetPivotData();
    ViewBag.data = data;
    return View();
}

Meanwhile, you can also achieve this in UI by invoking showConditionalFormattingDialog method on an external button click which is shown in the below code sample.

razor
Formatting.cs
@using Syncfusion.EJ2.PivotView

@Html.EJS().Button("conditional-formatting-btn").Content("Apply Formatting").IsPrimary(true).Render()
@Html.EJS().PivotView("PivotGrid").Width("100%").Height("300").DataSource(dataSource => dataSource.Data((IEnumerable<object>)ViewBag.Data).ExpandAll(false).EnableSorting(true)
           .DrilledMembers(drilledmembers =>
           {
               drilledmembers.Name("Country").Items(ViewBag.drilledMembers).Add();
           })
           .Rows(rows =>
           {
               rows.Name("Country").Add();
               rows.Name("Products").Add();
           })
           .Columns(columns =>
           {
               columns.Name("Year").Add();
               columns.Name("Order_Source").Caption("Order Source").Add();
           })
           .Values(values =>
           {
               values.Name("In_Stock").Caption("In Stock").Add();
               values.Name("Sold").Caption("Units Sold").Add();
           })
           .Filters(filters =>
           {
               filters.Name("Product_Categories").Caption("Product Categories").Add();
           })
           .ConditionalFormatSettings( format =>
           {
               format.Conditions(Condition.LessThan).Measure("In Stock").Value1(5000).Style( style => { style.BackgroundColor("#80cbc4").Color("black").FontFamily("Tahoma").FontSize("12px"); }).Add();
               format.Conditions(Condition.Between).Measure("Units Sold").Value1(3400).Value2(40000).Style(style => { style.BackgroundColor("#f48fb1").Color("black").FontFamily("Tahoma").FontSize("12px"); }).Add();
           })
           ).AllowConditionalFormatting(true).Render()

<script>
    document.getElementById("conditional-formatting-btn").addEventListener('click', function () {
        var pivotGridObj = document.getElementById("PivotGrid").ej2_instances[0];
        pivotGridObj.conditionalFormattingModule.showConditionalFormattingDialog();
    });
</script>
public ActionResult Index()
{
    var data = GetPivotData();
    ViewBag.data = data;
    return View();
}