|
1 | | -# How-to-clear-all-rows-in-Flutter-DataTable |
2 | | -This demo shows how to clear all rows in Flutter DataTable. |
| 1 | +# How to clear all rows in Flutter DataTable (SfDataGrid) ? |
| 2 | + |
| 3 | +In this article, we will show you how to clear all rows 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 necessary properties. The SfDataGrid relies on DataGridSource to retrieve row data. The number of rows and row selection depend on [DataGridSource.rows](https://pub.dev/documentation/syncfusion_flutter_datagrid/latest/datagrid/DataGridSource/rows.html). To remove all rows, set DataGridSource.rows to an empty list ([]). After clearing the rows, call [notifyListeners()](https://pub.dev/documentation/syncfusion_flutter_datagrid/latest/datagrid/DataGridSourceChangeNotifier/notifyListeners.html) to update the SfDataGrid and reflect the changes in the UI, ensuring smooth updates, especially during CRUD operations. |
| 6 | + |
| 7 | +```dart |
| 8 | +@override |
| 9 | + Widget build(BuildContext context) { |
| 10 | + return Scaffold( |
| 11 | + appBar: AppBar(title: const Text('Syncfusion Flutter DataGrid')), |
| 12 | + body: Column( |
| 13 | + children: [ |
| 14 | + TextButton( |
| 15 | + onPressed: () { |
| 16 | + employeeDataSource._employeeData = []; |
| 17 | + employeeDataSource.notifyListeners(); |
| 18 | + }, |
| 19 | + child: Text('Clear rows'), |
| 20 | + ), |
| 21 | + Expanded( |
| 22 | + child: SfDataGrid( |
| 23 | + source: employeeDataSource, |
| 24 | + selectionMode: SelectionMode.multiple, |
| 25 | + columnWidthMode: ColumnWidthMode.fill, |
| 26 | + columns: <GridColumn>[ |
| 27 | + GridColumn( |
| 28 | + columnName: 'id', |
| 29 | + label: Container( |
| 30 | + padding: EdgeInsets.all(16.0), |
| 31 | + alignment: Alignment.center, |
| 32 | + child: Text('ID'), |
| 33 | + ), |
| 34 | + ), |
| 35 | + GridColumn( |
| 36 | + columnName: 'name', |
| 37 | + label: Container( |
| 38 | + padding: EdgeInsets.all(8.0), |
| 39 | + alignment: Alignment.center, |
| 40 | + child: Text('Name'), |
| 41 | + ), |
| 42 | + ), |
| 43 | + GridColumn( |
| 44 | + columnName: 'designation', |
| 45 | + label: Container( |
| 46 | + padding: EdgeInsets.all(8.0), |
| 47 | + alignment: Alignment.center, |
| 48 | + child: Text('Designation', overflow: TextOverflow.ellipsis), |
| 49 | + ), |
| 50 | + ), |
| 51 | + GridColumn( |
| 52 | + columnName: 'salary', |
| 53 | + label: Container( |
| 54 | + padding: EdgeInsets.all(8.0), |
| 55 | + alignment: Alignment.center, |
| 56 | + child: Text('Salary'), |
| 57 | + ), |
| 58 | + ), |
| 59 | + ], |
| 60 | + ), |
| 61 | + ), |
| 62 | + ], |
| 63 | + ), |
| 64 | + ); |
| 65 | + } |
| 66 | +``` |
| 67 | + |
| 68 | +You can download this example on [GitHub](https://github.com/SyncfusionExamples/How-to-clear-all-rows-in-Flutter-DataTable). |
0 commit comments