sortedColumns property

List<SortColumnDetails> sortedColumns
inherited

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:

  • SfDataGrid.allowSorting – which allows users to sort the columns in SfDataGrid.
  • GridColumn.allowSorting - which allows users to sort the corresponding column in SfDataGrid.
  • DataGridSource.sort - call this method when you are adding the SortColumnDetails programmatically to DataGridSource.sortedColumns.

Implementation

List<SortColumnDetails> get sortedColumns => _sortedColumns;