Articles in this section
Category / Section

How to customize the cell style based on the condition in React Grid?

3 mins read

This article explains how to customize the cell style based on the condition in React Grid.

Customize the Cell style

You can apply cell style dynamically (conditionally) based on the data source value using Grid’s querycellInfo event.

 

In the below code example, cells are styled (text color) based on OrderID column value in queryCellInfo event by setting cell.style property.

 

Index.js

import { render } from 'react-dom';
import './index.css';
import * as React from 'react';
import { GridComponent, ColumnsDirective, ColumnDirective } from '@syncfusion/ej2-react-grids';
import { orderDetails } from './data';
import { SampleBase } from './sample-base';
export class Default extends SampleBase {
 
  queryCellInfo(args) {
    if (args.column.field == 'OrderID') {
      if (args.data.OrderID % 2 == 0) { //based on condition we have set the font color to the cell 
        args.cell.style.color = 'red';
      }
      else {
        args.cell.style.color = 'blue';
      }
    }
  }
 
    render() {
        return (<div className='control-pane'>
        <div className='control-section'>
          <GridComponent dataSource={orderDetails} height='350' queryCellInfo={this.queryCellInfo.bind(this)}>
            <ColumnsDirective>
              <ColumnDirective field='OrderID' headerText='Order ID' width='120' textAlign='Right'></ColumnDirective>
              <ColumnDirective field='CustomerName' headerText='Customer Name' width='150'></ColumnDirective>
              <ColumnDirective field='OrderDate' headerText='Order Date' width='130' format='yMd' textAlign='Right'/>
              <ColumnDirective field='Freight' headerText='Freight' width='120' format='C2' textAlign='Right'/>
              <ColumnDirective field='ShippedDate' headerText='Shipped Date' width='130' format='yMd' textAlign='Right'></ColumnDirective>
              <ColumnDirective field='ShipCountry' headerText='Ship Country' width='150'></ColumnDirective>
            </ColumnsDirective>
          </GridComponent>
        </div>
      </div>);
    }
}
 
render(<Default />, document.getElementById('sample'));
 

 

Customize the Cell style in React Grid.

 

View Sample in Stackbliz

 

See also

Customize the row style based on the condition in React Grid


Conclusion

I hope you enjoyed learning about how to customize the cell style based on the condition in React Grid.

You can refer to our React Grid feature tour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications. You can also explore our React Grid Example to understand how to create and manipulate data.

For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our other controls.

If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forumsDirect-Trac, or feedback portal. We are always happy to assist you!


Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please  to leave a comment
Access denied
Access denied