Articles in this section
Category / Section

How to Update Cell Value in Selected Row of Flutter DataTable?

2 mins read

In this article, we will show you how to update a cell value in the selected row of Flutter DataTable.

Initialize the SfDataGrid with the necessary properties. You can access the currently selected row using DataGridController.selectedRow. The row index is determined using DataGridSource.rows, which identifies the position of the selected row. The getCells() method returns a list of cells in the row, allowing you to update specific cells (e.g., ID and Name). After modifying a cell’s value, call the notifyDataSourceListeners method with the RowColumnIndex argument to specify the corresponding row and column index. This ensures that only the updated cell is refreshed in the DataGrid.

TextButton(
    onPressed: () {
    if (dataGridController.selectedRow != null) {
        final selectedRow = dataGridController.selectedRow!;
        final rowIndex =
            employeeDataSource._employeeData.indexOf(selectedRow);

        if (rowIndex >= 0) {
        final List<DataGridCell> cells =
            employeeDataSource._employeeData[rowIndex].getCells();

        // Find the index of the 'id' column in the row.
        final int colIndex =
            cells.indexWhere((cell) => cell.columnName == 'id');
        if (colIndex != -1) {
            employees[rowIndex].id = 100;
            employeeDataSource._employeeData[rowIndex]
                    .getCells()[colIndex] =
                const DataGridCell<int>(columnName: 'id', value: 100);
            employeeDataSource.updateDataGridSource(
                rowColumnIndex: RowColumnIndex(rowIndex, colIndex));
          }
        }
    }
    },
    child: const Text('Cell value')
),

You can download this example on GitHub.

Conclusion

I hope you enjoyed learning how to update cell value in selected row of Flutter DataTable.

You can refer to our Flutter Data Table 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 Data Table 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