When datagrid navigationMode set to navigationMode.row, have moveCurrentRowTo method to use?

Hi~

When datagrid set navigationMode to navigationMode.row can't use moveCurrentCellTo.

Can we have method like  moveCurrentRowTo to use 
? thank you!


 


3 Replies

AK Ashok Kuvaraja Syncfusion Team May 3, 2022 07:33 AM UTC

Hi Jimmy,

On the basis of your requirement, you are trying to move the current row to a specific row index. We don't have a method `moveCurrentRowTo` to achieve this by default. But, you can achieve this by setting the move row index to the `DataGridControler.selectedIndex` property and calling `DataGridController.ScrollToRow()` to move the current row to that specified row index. Please refer to the following code snippets,


import 'dart:core';

import 'package:flutter/material.dart';

import 'package:syncfusion_flutter_datagrid/datagrid.dart';


void main() {

  runApp(MyApp());

}


class MyApp extends StatelessWidget {

  @override

  Widget build(BuildContext context) {

    return MaterialApp(

      title: 'Syncfusion DataGrid Demo',

      theme: ThemeData(primarySwatch: Colors.blue),

      home: SimpleDataGridSample(),

    );

  }

}


class SimpleDataGridSample extends StatefulWidget {

  const SimpleDataGridSample({Key? key}) : super(key: key);


  @override

  _SimpleDataGridSampleState createState() => _SimpleDataGridSampleState();

}


class _SimpleDataGridSampleState extends State<SimpleDataGridSample> {

  late DataGridController controller;

  late EmployeeDataSource employeeDataGridSource;


  @override

  void initState() {

    super.initState();

    employeeDataGridSource =

        EmployeeDataSource(employeeData: getEmployeeData());

    controller = DataGridController();

  }


  void moveCurrentRowTo({required int rowIndex}) {

    controller

      ..selectedIndex = rowIndex

      ..scrollToRow(rowIndex.toDouble());

  }


  @override

  Widget build(BuildContext context) {

    return Scaffold(

      appBar: AppBar(),

      body: Column(

        children: [

          Expanded(

            child: SfDataGrid(

              controller: controller,

              source: employeeDataGridSource,

              selectionMode: SelectionMode.multiple,

              navigationMode: GridNavigationMode.row,

              columns: <GridColumn>[

                GridColumn(

                    columnName: 'id',

                    label: Container(

                        padding: EdgeInsets.all(16.0),

                        alignment: Alignment.center,

                        child: Text(

                          'ID',

                        ))),

                GridColumn(

                    columnName: 'name',

                    label: Container(

                        padding: EdgeInsets.all(8.0),

                        alignment: Alignment.center,

                        child: Text('Name'))),

                GridColumn(

                    columnName: 'designation',

                    label: Container(

                        padding: EdgeInsets.all(8.0),

                        alignment: Alignment.center,

                        child: Text(

                          'Designation',

                          overflow: TextOverflow.ellipsis,

                        ))),

                GridColumn(

                    columnName: 'salary',

                    label: Container(

                        padding: EdgeInsets.all(8.0),

                        alignment: Alignment.center,

                        child: Text('Salary'))),

              ],

            ),

          ),

          MaterialButton(

            child: Text(

              'Move current row',

              style: TextStyle(color: Colors.white),

            ),

            color: Colors.blue,

            onPressed: () {

              moveCurrentRowTo(rowIndex: 10);

            },

          )

        ],

      ),

    );

  }


  List<Employee> getEmployeeData() {

    return [

      Employee(10001, 'James', 'Project Lead', 20000),

      Employee(10002, 'Kathryn', 'Manager', 30000),

      Employee(10003, 'Lara', 'Developer', 15000),

      Employee(10004, 'Michael', 'Designer', 15000),

      Employee(10005, 'Martin', 'Developer', 15000),

      Employee(10006, 'Newberry', 'Developer', 15000),

      Employee(10007, 'Balnc', 'Developer', 15000),

      Employee(10008, 'Perry', 'Developer', 15000),

      Employee(10009, 'Gable', 'Developer', 15000),

      Employee(10010, 'Grimes', 'Developer', 15000),

      Employee(10011, 'Kathryn', 'Manager', 30000),

      Employee(10012, 'Lara', 'Developer', 15000),

      Employee(10013, 'Michael', 'Designer', 15000),

      Employee(10014, 'Martin', 'Developer', 15000),

      Employee(10015, 'Newberry', 'Developer', 15000),

      Employee(10016, 'Balnc', 'Developer', 15000),

      Employee(10017, 'Perry', 'Developer', 15000),

      Employee(10018, 'Gable', 'Developer', 15000),

      Employee(10019, 'Grimes', 'Developer', 15000),

      Employee(10020, 'Kathryn', 'Manager', 30000),

    ];

  }

}


/// Custom business object class which contains properties to hold the detailed

/// information about the employee which will be rendered in datagrid.

class Employee {

  /// Creates the employee class with required details.

  Employee(this.id, this.name, this.designation, this.salary);


  /// Id of an employee.

  final int id;


  /// Name of an employee.

  final String name;


  /// Designation of an employee.

  final String designation;


  /// Salary of an employee.

  final int salary;

}


/// An object to set the employee collection data source to the datagrid. This

/// is used to map the employee data to the datagrid widget.

class EmployeeDataSource extends DataGridSource {

  /// Creates the employee data source class with required details.

  EmployeeDataSource({required List<Employee> employeeData}) {

    _employeeData = employeeData

        .map<DataGridRow>((e) => DataGridRow(cells: [

              DataGridCell<int>(columnName: 'id', value: e.id),

              DataGridCell<String>(columnName: 'name', value: e.name),

              DataGridCell<String>(

                  columnName: 'designation', value: e.designation),

              DataGridCell<int>(columnName: 'salary', value: e.salary),

            ]))

        .toList();

  }


  List<DataGridRow> _employeeData = [];


  @override

  List<DataGridRow> get rows => _employeeData;


  @override

  DataGridRowAdapter buildRow(DataGridRow row) {

    return DataGridRowAdapter(

        cells: row.getCells().map<Widget>((e) {

      return Container(

        alignment: Alignment.center,

        padding: EdgeInsets.all(8.0),

        child: Text(e.value.toString()),

      );

    }).toList());

  }

}



If your requirement is still not resolved, please let us know with more details.


Regards,

Ashok K



JH Jimmy Huang May 4, 2022 08:42 AM UTC

Hi~  Ashok,
Thank you! :)



TP Tamilarasan Paranthaman Syncfusion Team May 5, 2022 04:20 AM UTC

Hi Jimmy,


We are glad that the provided response meets your requirement. Please let us know if you need further assistance. As always, we are happy to help you out. 


Regards,

Tamilarasan


Loader.
Up arrow icon