Setting the Initial Scroll Position - SfDataGrid

Hi,

Greeting,

in my case, i get data from firebase using Streambuilder, DataGrid's last row have newest information.

so i want to set Initial scroll position to end. and also automatically move end if i got new row / rows. 


Thanks,


1 Reply

TP Tamilarasan Paranthaman Syncfusion Team April 27, 2022 11:29 AM UTC

Hi Aravindhan,


Greetings from Syncfusion.


You can achieve your requirement by using programmatic scrolling. While adding a new row in the DataGrid, scroll to that row using scrollToRow method from the DataGridController. We have prepared a simple sample for your scenario. In that sample, we have added a new row at the end by clicking the button, and then it used the scrollToRow method to scroll to the last row. Please check the following code snippet and sample.


Code snippet:

  final DataGridController dataGridController = DataGridController();

 

  @override

  Widget build(BuildContext context) {

    return Scaffold(

      appBar: AppBar(

        title: const Text('Flutter SfDataGrid'),

      ),

      body: Column(

        children: [

          ElevatedButton(

              onPressed: () {

                int index = _employeeDataSource.dataGridRow.length;

                _employeeDataSource.dataGridRow.insert(

                    index,

                    const DataGridRow(cells: [

                      DataGridCell(value: 20000, columnName: 'id'),

                      DataGridCell(value: 'Tom Bass', columnName: 'name'),

                      DataGridCell(value: 'Developer', columnName: 'designation'),

                      DataGridCell(value: 20000, columnName: 'salary')

                    ]));

                _employeeDataSource.updateDataGridSource();

 

                WidgetsBinding.instance!.addPostFrameCallback((details) {

                  dataGridController.scrollToRow(

                      _employeeDataSource.dataGridRow.length.toDouble());

                });

              },

              child: const Text('Add a new row')),

          Expanded(

            child: SfDataGrid(

              source: _employeeDataSource,

              columns: getColumns,

              controller: dataGridController,

            ),

          ),

        ],

      ),

    );

  }


Sample Link: https://www.syncfusion.com/downloads/support/directtrac/general/ze/sample816025162


We hope this helps. Please let us know if you need any further assistance.


Regards,

Tamilarasan


Loader.
Up arrow icon