Articles in this section
Category / Section

How to display row indexes like Excel in Flutter DataTable?

2 mins read

In this article, we will show you how to display row indexes like Excel in Flutter DataTable.

Initialize the SfDataGrid widget with all the required properties. you can display indexes ranging from 1 to the visible row count by creating a separate column as the first column to show the count of rows. In the buildRow method, add the respective index to the first column by utilizing the effective rows collection. Obtaining the index from the effective rows ensures it ranges from 1 to the visible row count, even when filtering or sorting is applied.

class EmployeeDataSource extends DataGridSource {
…

  @override
  DataGridRowAdapter buildRow(DataGridRow row) {
    return DataGridRowAdapter(
        cells: row.getCells().map<Widget>((dataGridCell) {
      return Container(
        alignment: Alignment.center,
        padding: const EdgeInsets.symmetric(horizontal: 8.0),
        child: dataGridCell.columnName == 'Index'
            ? getSerialNumber(row)
            : Text(dataGridCell.value.toString()),
      );
    }).toList());
  }

  Text getSerialNumber(DataGridRow row) {
    // Get the index of the current row
    // Add header count to the index, here 1 is added
    int serialNumber = effectiveRows.indexOf(row) + 1;
    return Text((serialNumber.toString()));
  }
}

You can download this example on GitHub.

Conclusion

I hope you enjoyed learning about how to display row indexes like Excel 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 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 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