Articles in this section
Category / Section

How to Get the Cell Value when Tapping the Cells in Flutter DataGrid?

2 mins read

The Syncfusion Flutter DataTable widget allows you to handle the interactions for customization. You can refer to this documentation, to know more about the interactions callbacks supported in DataGrid. The onCellTap callback can be used to retrieve the value of a cell when it is tapped in Flutter DataGrid .

 

Initialize the Flutter DataGrid  widget with all the required properties. The information about the tapped cell (i.e., rowColumnIndex, column, localPosition, and globalPosition) can be accessed using the DataGridCellTapDetails, which is passed as a parameter in onCellTap callback.

 

You can get the tapped cell value from the effectiveRows collection by using the RowColumnIndex.rowIndex property.

 

  late EmployeeDataGridSource _employeeDataGridSource;
  List<Employee> _employees = <Employee>[];
 
  @override
  void initState() {
    super.initState();
    _employees = getEmployeeData();
    _employeeDataGridSource = EmployeeDataGridSource(employees: _employees);
  }
 
  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
            title: const Text('Syncfusion Flutter DataGrid Demo',
                overflow: TextOverflow.ellipsis)),
        body: SfDataGrid(
            source: _employeeDataGridSource,
            onCellTap: (DataGridCellTapDetails details) {
              showDialog(
                  context: context,
                  builder: (BuildContext context) => AlertDialog(
                      title: const Text('Tapped Content'),
                      actions: <Widget>[
                        TextButton(
                            onPressed: () => Navigator.pop(context),
                            child: const Text('OK'))
                      ],
                      content: Text(_employeeDataGridSource
                          .effectiveRows[details.rowColumnIndex.rowIndex - 1]
                          .getCells()[details.rowColumnIndex.columnIndex]
                          .value
                          .toString())));
            },
            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'))),
            ]));
  }

 

get_cellvalue_when_tapping

You can download this example on GitHub.

 

Conclusion

I hope you enjoyed learning about how to get the cell value when tapping the cells in Flutter DataGrid.

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
Please  to leave a comment
Access denied
Access denied