How to get the selected row data in Flutter DataTable?
In this article, we will show you how to get the selected row data in Flutter DataTable.
Step 1:
Initialize the SfDataGrid widget with all the required properties. The DataGrid provides two callbacks for selection: onSelectionChanging and onSelectionChanged. These callbacks are triggered when rows are added or removed from the selection. Both callbacks have two parameters: addedRows and removedRows. This allows us to get the details of selected rows while performing a selection using the onSelectionChanged callback’s addedRows list.
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Syncfusion Flutter DataGrid'),
),
body: SfDataGrid(
source: employeeDataSource,
columns: getColumns,
columnWidthMode: ColumnWidthMode.fill,
selectionMode: SelectionMode.multiple,
onSelectionChanged:
(List<DataGridRow> addedRows, List<DataGridRow> removedRows) {
// Selected rows while performing a selection.
DataGridRow selectedRow = addedRows[0];
},
),
);
}
Step 2:
To retrieve the details of the selected rows entirely, use the controller property. Create an instance of the DataGridController and assign it to the controller property. The selectedRow property returns the selected DataGridRow, and the selectedIndex property returns the index of the selected row in the SfDataGrid.
final DataGridController _dataGridController = DataGridController();
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Syncfusion Flutter DataGrid'),
),
body: Column(
children: [
TextButton(
child: const Text('Get Selection Information'),
onPressed: () {
//SelectedIndex
var _selectedIndex = _dataGridController.selectedIndex;
//SelectedRow
var _selectedRow = _dataGridController.selectedRow;
//SelectedRows
var _selectedRows = _dataGridController.selectedRows;
}),
Expanded(
child: SfDataGrid(
source: employeeDataSource,
controller: _dataGridController,
columns: getColumns,
columnWidthMode: ColumnWidthMode.fill,
selectionMode: SelectionMode.multiple,
),
),
],
),
);
}
You can download the example from GitHub.
Conclusion
I hope you enjoyed learning about how to get the selected row data 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!