We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

Finding externally added row with data criteria, make it visible and selected - focused

Hi,

  1. We are adding externaly a new data object in the underline data list,
  2. We update the DataGridSource
  3. buildGridRows();

    notifyListeners();

  4. We find in DataGridRows with a For loop, the position of the new row applying data criteria (the key on the cells). (it can be sorted or not). Is there a better way ?
  5. We can't make the specific row visible and selected
    Using any of those (selection mode is single   -  no paging)
  6. // userGridController.selectedIndex= pos;

    userGridController.selectedRow = _usersDataSource.dataGridRows[pos];
    // userGridController.scrollToRow(pos.toDouble());

    What is the best way to do the steps 2 ,3,4

    thank you


2 Replies 1 reply marked as answer

TP Tamilarasan Paranthaman Syncfusion Team October 20, 2022 07:37 AM

Hi Stefan,


As per your requirement, you can achieve that using the DataGridController. After adding the DataGrid row into the underline collection call the notifyListeners from the DataGridSource class, find the added row index from the DataGridSource.effectiveRows property. Then, assign that index to the DataGridController.selectedIndex for selecting the current added row and using the DataGridController.scrollToRow property for scrolling into the added row. We have prepared a sample for that. Please check the following sample and code snippet.


@override

Widget build(BuildContext context) {

return Scaffold(

appBar: AppBar(

title: const Text('Syncfusion Flutter DataGrid'),

),

body: Column(

children: [

Card(

margin: const EdgeInsets.all(20),

child: ElevatedButton(

onPressed: (() {

DataGridRow dataGridRow = DataGridRow(cells: [

DataGridCell(

columnName: 'ID', value: random.nextInt(2000)),

const DataGridCell(

columnName: 'Name', value: 'Jack'),

const DataGridCell(

columnName: 'Designation', value: 'C.E.O'),

const DataGridCell(columnName: 'Salary', value: 30000),

]);

_employeeDataSource.datagridRows.add(dataGridRow);

_employeeDataSource.updateDataGridSource();

int index =

_employeeDataSource.effectiveRows.indexOf(dataGridRow);

dataGridController.selectedIndex = index;

dataGridController

.scrollToRow(dataGridController.selectedIndex.toDouble());

}),

child: const Text('Add Row')),

),

Expanded(

child: SfDataGrid(

source: _employeeDataSource,

controller: dataGridController,

columnWidthMode: ColumnWidthMode.fill,

selectionMode: SelectionMode.single,

columns: getColumns)),

],

),

);

}

}

In DataGridSource class:

class EmployeeDataSource extends DataGridSource {

void updateDataGridSource() {

notifyListeners();

}

}


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


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


Regards,

Tamilarasan


Attachment: Demo_f2e78386.zip


Marked as answer

ST Stefan Tsalapatis October 20, 2022 09:17 AM

Thanks 


Loader.
Live Chat Icon For mobile
Up arrow icon