Articles in this section

How to ignore the last row from being sorted in Flutter DataTable (SfDataGrid)?

The Syncfusion® Flutter DataGrid supports column sorting based on custom logic. You can override the DataGridSource.performSorting method to handle sorting completely in your way. This method is called whenever the column sorting is performed.

 

You can use the argument that passes the list of DataGridRow and remove the last row from the rows collection and pass it to the superclass. Add the last row, once the sorting is completed. So that you can ignore the last row when sorting.

 

class EmployeeDataGridSource extends DataGridSource {
  EmployeeDataGridSource(
      {required List<Employee> employeeData}) {
    _dataGridRows = employeeData
        .map<DataGridRow>((e) => DataGridRow(cells: [
              DataGridCell<int>(columnName: 'id', value: e.id),
              DataGridCell<String>(columnName: 'name', value: e.name),
              DataGridCell<String>(
                  columnName: 'designation', value: e.designation),
              DataGridCell<int>(columnName: 'salary', value: e.salary),
            ]))
        .toList();
  }
 
  List<DataGridRow> _dataGridRows = [];
 
  @override
  List<DataGridRow> get rows => _dataGridRows;
 
  @override
  void performSorting(List<DataGridRow> rows) {
    final DataGridRow lastRow= rows.last;
    rows.removeLast();
    super.performSorting(rows);
    rows.add(lastRow);
  }
 
  @override
  DataGridRowAdapter buildRow(DataGridRow row) {
    return DataGridRowAdapter(
        cells: row.getCells().map<Widget>((e) {
      return Container(
        alignment: Alignment.center,
        padding: const EdgeInsets.all(8.0),
        child: Text(e.value?.toString() ?? ''),
      );
    }).toList());
  }
}

 

ignore_last_row_from_being_sorted

 
View the GitHub sample here.

You can also click this link to know more about the sorting feature.

 

Conclusion

I hope you enjoyed learning about how to ignore the last row from being sorted in Flutter DataTable (SfDataGrid).

You can refer to our Flutter DataGrid 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  Flutter DataGrid 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)
Access denied
Access denied