sortedColumns property Null safety

List<SortColumnDetails> sortedColumns

The collection of SortColumnDetails objects to sort the columns in SfDataGrid.

You can use this property to sort the columns programmatically also. Call sort method after you added the column details in sortedColumns programmatically.

The following example show how to sort the columns on datagrid loading,

@override
Widget build(BuildContext context) {
  return Scaffold(
    appBar: AppBar(
      title: const Text('Syncfusion Flutter DataGrid'),
    ),
    body: Column(
      children: [
        TextButton(
          child: Text('Click'),
          onPressed: () {
            _employeeDataSource.sortedColumns
                .add(SortColumnDetails('id', SortDirection.ascending));
            _employeeDataSource.sort();
          },
        ),
        SfDataGrid(
          source: _employeeDataSource,
          allowSorting: true,
          columns: <GridColumn>[
              GridColumn(columnName: 'id', label: Text('ID')),
              GridColumn(columnName: 'name', label: Text('Name')),
              GridColumn(columnName: 'designation', label: Text('Designation')),
              GridColumn(columnName: 'salary', label: Text('Salary')),
          ],
        ),
      ],
    ),
  );
}

See also:

Implementation

List<SortColumnDetails> get sortedColumns => _sortedColumns;