Articles in this section
Category / Section

How to show sum of a column in Flutter DataTable (SfDataGrid)?

2 mins read

The Syncfusion® Flutter DataTable widget allows you to display concise information about the rows by using the table summary rows. The summary value of a column is calculated based on all the rows in the DataGridSource.rows property.

 

The following steps explain how to show the table summary row in Syncfusion® Flutter DataTable:

 

STEP 1: Initialize the SfDataGrid widget with all the required properties and set SfDataGrid.tableSummaryRows property by defining the summary columns to the GridTableSummaryRow.

 

You can add the GridSummaryColumn to the GridTableSummaryRow.columns collection. You can also show the summary in a whole row i.e., all the columns are spanned. Make sure that the summary columns’ columnName property value and columns’ columnName property value should be the same.

 

You can show the summary for a specific column along with the title, which spans across three columns by using the titleColumnSpan and setting the showSummaryInRow property as false.

 

late EmployeeDataSource _employeeDataSource;
List<Employee> _employees = <Employee>[];
 
@override
void initState() {
  super.initState();
  _employees = getEmployeeData();
  _employeeDataSource = EmployeeDataSource(employees: _employees);
}
 
@override
Widget build(BuildContext context) {
  return Scaffold(
      appBar: AppBar(title: const Text('Syncfusion Flutter DataGrid')),
      body: SfDataGrid(
          source: _employeeDataSource,
          columnWidthMode: ColumnWidthMode.fill,
          tableSummaryRows: [
            GridTableSummaryRow(
                showSummaryInRow: false,
                title: 'Total Salary Amount:',
                titleColumnSpan: 3,
                columns: [
                  const GridSummaryColumn(
                      name: 'Sum',
                      columnName: 'salary',
                      summaryType: GridSummaryType.sum)
                ],
                position: GridTableSummaryRowPosition.bottom),
          ],
          columns: [
            GridColumn(
                columnName: 'id',
                label: Container(
                    padding: const EdgeInsets.symmetric(horizontal: 16.0),
                    alignment: Alignment.center,
                    child: const Text('ID'))),
            GridColumn(
                columnName: 'name',
                label: Container(
                    padding: const EdgeInsets.symmetric(horizontal: 16.0),
                    alignment: Alignment.center,
                    child: const Text('Name'))),
            GridColumn(
                columnName: 'designation',
                label: Container(
                    padding: const EdgeInsets.symmetric(horizontal: 16.0),
                    alignment: Alignment.center,
                    child: const Text('Designation',
                        overflow: TextOverflow.ellipsis))),
            GridColumn(
                columnName: 'salary',
                label: Container(
                    padding: const EdgeInsets.symmetric(horizontal: 16.0),
                    alignment: Alignment.center,
                    child: const Text('Salary'))),
          ]));
}

 

STEP 2: Create Employee DataGridSource class by extending the DataGridSource for mapping data to the SfDataGrid. Override the buidTableSummaryCellWidget method in the DataGridSource and return the required widget to display the summary value in it.

 

DataGridSource for mapping data to the SfDataGrid. Override the buidTableSummaryCellWidget method in the DataGridSource class and return the required widget to display the summary value in it.

 

class EmployeeDataSource extends DataGridSource {
  EmployeeDataSource({required List<Employee> employees}) {
    dataGridRows = employees
        .map<DataGridRow>((dataGridRow) => DataGridRow(cells: [
              DataGridCell<int>(columnName: 'id', value: dataGridRow.id),
              DataGridCell<String>(columnName: 'name', value: dataGridRow.name),
              DataGridCell<String>(
                  columnName: 'designation', value: dataGridRow.designation),
              DataGridCell<int>(
                  columnName: 'salary', value: dataGridRow.salary),
            ]))
        .toList();
  }
 
  List<DataGridRow> dataGridRows = [];
 
  @override
  List<DataGridRow> get rows => dataGridRows;
 
  @override
  Widget? buildTableSummaryCellWidget(
      GridTableSummaryRow summaryRow,
      GridSummaryColumn? summaryColumn,
      RowColumnIndex rowColumnIndex,
      String summaryValue) {
    return Container(
        padding: const EdgeInsets.all(15.0), child: Text(summaryValue));
  }
 
  @override
  DataGridRowAdapter? buildRow(DataGridRow row) {
    return DataGridRowAdapter(
        cells: row.getCells().map<Widget>((dataGridCell) {
      return Container(
          alignment: Alignment.center,
          padding: const EdgeInsets.symmetric(horizontal: 16.0),
          child: Text(
            dataGridCell.value.toString(),
            overflow: TextOverflow.ellipsis,
          ));
    }).toList());
  }
}

 

show_sum_of_a_column

 

View the GitHub sample here.

 

Conclusion

I hope you enjoyed learning about how to show the sum of a column 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)
Please  to leave a comment
Access denied
Access denied