|
1 | | -# How-to-delete-a-row-when-long-pressing-on-a-data-row-in-Flutter-DataTable. |
2 | | -How to delete a row when long pressing on a data row in Flutter DataTable. |
| 1 | +# How to delete a row when long pressing on a data row in Flutter DataTable (SfDataGrid)?. |
| 2 | + |
| 3 | +In this article, we will show you how to delete a row when long pressing on a data row in [Flutter DataTable](https://www.syncfusion.com/flutter-widgets/flutter-datagrid). |
| 4 | + |
| 5 | +Initialize the [SfDataGrid](https://pub.dev/documentation/syncfusion_flutter_datagrid/latest/datagrid/SfDataGrid-class.html) widget with all the necessary properties. The [SfDataGrid.onCellLongPress](https://pub.dev/documentation/syncfusion_flutter_datagrid/latest/datagrid/SfDataGrid/onCellLongPress.html) 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](https://pub.dev/documentation/syncfusion_flutter_datagrid/latest/datagrid/DataGridSource/rows.html) collection. After removing the row, update or refresh the DataGrid when the underlying data is updated by using [notifyListeners](https://api.flutter.dev/flutter/foundation/ChangeNotifier/notifyListeners.html). |
| 6 | + |
| 7 | +```dart |
| 8 | + @override |
| 9 | + Widget build(BuildContext context) { |
| 10 | + return Scaffold( |
| 11 | + appBar: AppBar( |
| 12 | + title: const Text('Syncfusion Flutter DataGrid'), |
| 13 | + ), |
| 14 | + body: SfDataGrid( |
| 15 | + onCellLongPress: (details) { |
| 16 | + // Restict the header rows. |
| 17 | + if (details.rowColumnIndex.rowIndex > 0) { |
| 18 | + // Remove header count to the index, here 1 is minus. |
| 19 | + DataGridRow deleteRow = employeeDataSource |
| 20 | + .effectiveRows[details.rowColumnIndex.rowIndex - 1]; |
| 21 | +
|
| 22 | + employeeDataSource._employeeData.remove(deleteRow); |
| 23 | + employeeDataSource.refreshDataGrid(); |
| 24 | + } |
| 25 | + }, |
| 26 | + source: employeeDataSource, |
| 27 | + columnWidthMode: ColumnWidthMode.fill, |
| 28 | + columns: <GridColumn>[ |
| 29 | + GridColumn( |
| 30 | + columnName: 'id', |
| 31 | + label: Container( |
| 32 | + padding: const EdgeInsets.all(16.0), |
| 33 | + alignment: Alignment.center, |
| 34 | + child: const Text( |
| 35 | + 'ID', |
| 36 | + ))), |
| 37 | + GridColumn( |
| 38 | + columnName: 'name', |
| 39 | + label: Container( |
| 40 | + padding: const EdgeInsets.all(8.0), |
| 41 | + alignment: Alignment.center, |
| 42 | + child: const Text('Name'))), |
| 43 | + GridColumn( |
| 44 | + columnName: 'designation', |
| 45 | + label: Container( |
| 46 | + padding: const EdgeInsets.all(8.0), |
| 47 | + alignment: Alignment.center, |
| 48 | + child: const Text( |
| 49 | + 'Designation', |
| 50 | + overflow: TextOverflow.ellipsis, |
| 51 | + ))), |
| 52 | + GridColumn( |
| 53 | + columnName: 'salary', |
| 54 | + label: Container( |
| 55 | + padding: const EdgeInsets.all(8.0), |
| 56 | + alignment: Alignment.center, |
| 57 | + child: const Text('Salary'))), |
| 58 | + ], |
| 59 | + ), |
| 60 | + ); |
| 61 | + } |
| 62 | +``` |
| 63 | + |
| 64 | +You can download this example on [GitHub](https://github.com/SyncfusionExamples/How-to-delete-a-row-when-long-pressing-on-a-data-row-in-Flutter-DataTable). |
0 commit comments