Blazor Grid Conditional Styling

    The Ignite UI for Blazor Conditional Styling feature in Blazor Grid allows custom styling on a row or cell level. The IgbGrid Conditional Styling functionality is used to visually emphasize or highlight data that meets certain criteria, making it easier for users to identify important information or trends within the grid.

    Grid Conditional Row Styling

    The IgbGrid component in Ignite UI for Blazor provides two ways to conditional styling of rows based on custom rules.

    Further in this topic we will cover both of them in more details.

    Using Row Classes

    You can conditionally style the IgbGrid rows by setting the RowClasses input and define custom rules.

    <IgbGrid AutoGenerate="true" Id="grid" Data="CustomersData" Name="grid" RowClassesScript="RowClassesHandler" @ref="grid">
    </IgbGrid>
    

    The RowClasses input accepts an object literal, containing key-value pairs, where the key is the name of the CSS class, while the value is either a callback function that returns a boolean, or boolean value.

    igRegisterScript("RowClassesHandler", () => {
        return {
            activeRow: (row) => row.index === 0
        };
    }, true);
    
    .activeRow {
        border: 2px solid #fc81b8;
        border-left: 3px solid #e41c77;
    }
    

    Demo

    Using Row Styles

    The IgbGrid control exposes the RowStyles property which allows conditional styling of the data rows. Similar to RowClasses it accepts an object literal where the keys are style properties and the values are expressions for evaluation. Also, you can apply regular styling (without any conditions).

    The callback signature for both RowStyles and RowClasses is:

    (row) => boolean
    

    Let's define our styles:

    igRegisterScript("WebGridRowStylesHandler", () => {
        return {
            'background': (row) => (+row.data['Change'] < 0 && +row.data['AnnualChange'] < 0) ? '#FF000088' : '#00000000',
            'border': (row) => (+row.data['Change'] < 0 && +row.data['AnnualChange'] < 0) ? '2px solid' : '1px solid',
            'border-color': (row) => (+row.data['Change'] < 0 && +row.data['AnnualChange'] < 0) ? '#FF000099' : '#E9E9E9'
        };
    }, true);
    
    <IgbGrid AutoGenerate="true" Id="grid" Data="CustomersData" Name="grid" RowStylesScript="WebGridRowStylesHandler" @ref="grid">
    </IgbGrid>
    

    Demo

    Grid Conditional Cell Styling

    Overview

    The IgbGrid component in Ignite UI for Blazor provides two ways to conditional styling of cells based on custom rules.

    • By setting the IgbColumn input CellClasses to an object literal containing key-value pairs. The key is the name of the CSS class, while the value is either a callback function that returns a boolean, or boolean value. The result is a convenient material styling of the cell.

    Using Cell Classes

    You can conditionally style the IgbGrid cells by setting the IgbColumn CellClasses input and define custom rules.

    <IgbColumn Field="BeatsPerMinute" CellClassesScript="CellClassesHandler">
    

    The CellClasses input accepts an object literal, containing key-value pairs, where the key is the name of the CSS class, while the value is either a callback function that returns a boolean, or boolean value.

    igRegisterScript("CellClassesHandler", () => {
        return {
            downFont: (rowData, columnKey, cellValue, rowIndex) => rowData[columnKey] <= 95,
            upFont: (rowData, columnKey, cellValue, rowIndex) => rowData[columnKey] > 95
        };
    }, true);
    
    .upFont {
        color: green !important;
    }
    
    .downFont {
        color: red !important;
    }
    

    Demo

    • By using the IgbColumn input CellStyles which accepts an object literal where the keys are style properties and the values are expressions for evaluation.

    The callback signature for both cellStyles and cellClasses is now changed to:

    (rowData, columnKey, cellValue, rowIndex) => boolean
    

    Using Cell Styles

    Columns expose the CellStyles property which allows conditional styling of the column cells. Similar to CellClasses it accepts an object literal where the keys are style properties and the values are expressions for evaluation. Also, you can apply regular styling with ease (without any conditions).

    Let's define our styles:

    igRegisterScript("WebGridCellStylesHandler", () => {
        return {
            background: (rowData, columnKey, cellValue, rowIndex) => rowIndex % 2 === 0 ? "#EFF4FD" : null,
            color: (rowData, columnKey, cellValue, rowIndex) => {
                if (columnKey === "Position") {
                    switch (cellValue) {
                        case "up": return "#28a745";
                        case "down": return "#dc3545";
                        case "current": return "#17a2b8"
                    }
                }
            }
        };
    }, true);
    
    <IgbColumn CellStylesScript="WebGridCellStylesHandler">
    </IgbColumn>
    

    Demo

    Known issues and limitations

    • If there are cells bind to the same condition (from different columns) and one cell is updated, the other cells won't be updated based on the new value, if the condition is met.

    API References

    Additional Resources

    Our community is active and always welcoming to new ideas.