computeCellWidth method Null safety

  1. @protected
double computeCellWidth(
  1. GridColumn column,
  2. DataGridRow row,
  3. Object? cellValue,
  4. TextStyle textStyle

Calculates the width of the cell based on the DataGridCell.value. You can override this method to perform the custom calculation for width.

If you want to calculate the width based on different TextStyle, you can override this method and return the super method with the required TextStyle. Set the custom ColumnSizer to SfDataGrid.columnSizer property.

The following example show how to pass the different TextStyle for width calculation,

class CustomColumnSizer extends ColumnSizer {
 double computeCellWidth(GridColumn column, DataGridRow row, Object cellValue,
     TextStyle textStyle) {
  TextStyle style = textStyle;
  if (column.columnName == 'Name')
   style = TextStyle(fontSize: 20, fontStyle: FontStyle.italic);
 return super.computeCellWidth(column, row, cellValue, style);

The auto size is calculated based on default TextStyle of the datagrid.


double computeCellWidth(GridColumn column, DataGridRow row, Object? cellValue,
    TextStyle textStyle) {
  final DataGridConfiguration dataGridConfiguration =
  final int rowIndex = grid_helper.resolveToRowIndex(dataGridConfiguration,

  return _calculateTextSize(
          column: column,
          value: cellValue,
          rowIndex: rowIndex,
          textStyle: textStyle,
          width: double.infinity)