Articles in this section
Category / Section

How to delete a row when long pressing in Flutter DataTable?

4 mins read

In this article, we will show you how to delete a row when long pressing on a data row in Flutter DataTable.

Initialize the SfDataGrid widget with all the necessary properties. The SfDataGrid.onCellLongPress event is triggered when a long press gesture with a primary button is recognized for a cell. Upon triggering onCellLongPress, you can obtain the rowColumnIndex. Using the rowIndex, you can then remove the corresponding row from the rows collection. After removing the row, update or refresh the DataGrid when the underlying data is updated by using notifyListeners.

 @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Syncfusion Flutter DataGrid'),
      ),
      body: SfDataGrid(
        onCellLongPress: (details) {
          // Restict the header rows.
          if (details.rowColumnIndex.rowIndex > 0) {
            //  Remove header count to the index, here 1 is minus.
            DataGridRow deleteRow = employeeDataSource
                .effectiveRows[details.rowColumnIndex.rowIndex - 1];

            employeeDataSource._employeeData.remove(deleteRow);
            employeeDataSource.refreshDataGrid();
          }
        },
        source: employeeDataSource,
        columnWidthMode: ColumnWidthMode.fill,
        columns: <GridColumn>[
          GridColumn(
              columnName: 'id',
              label: Container(
                  padding: const EdgeInsets.all(16.0),
                  alignment: Alignment.center,
                  child: const Text(
                    'ID',
                  ))),
          GridColumn(
              columnName: 'name',
              label: Container(
                  padding: const EdgeInsets.all(8.0),
                  alignment: Alignment.center,
                  child: const Text('Name'))),
          GridColumn(
              columnName: 'designation',
              label: Container(
                  padding: const EdgeInsets.all(8.0),
                  alignment: Alignment.center,
                  child: const Text(
                    'Designation',
                    overflow: TextOverflow.ellipsis,
                  ))),
          GridColumn(
              columnName: 'salary',
              label: Container(
                  padding: const EdgeInsets.all(8.0),
                  alignment: Alignment.center,
                  child: const Text('Salary'))),
        ],
      ),
    );
  }

View the GitHub sample here.

Conclusion

I hope you enjoyed learning about how to delete a row when long pressing on a data row in Flutter DataTable.

You can refer to our Flutter DataTable 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 DataTable 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 forums, Direct-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