Hi Roberto,
Greetings from Syncfusion.
We have checked your code snippet and your requirement. Since DataGrid generates the rows based on the DataGridSource class’s rows property, You have to either add the new DataGridRow based on your data to rows collection or regenerate the rows collection based on currently modified collection (event collection in your case) and call the notifyListeners method. Based on your code snippet, we are providing the prototype code to add data and refresh the datagrid.
|
class GridSource extends DataGridSource { GridSource(this._eventGrid)
{ buildDataGridRows(); void addDataGridRow(Event event) { rows.add(DataGridRow(cells: [ DataGridCell<int>(columnName: 'name',
value: event.eventName), DataGridCell<String>(columnName:
'start', value: event.eventStart.toString()), DataGridCell<String>(columnName:
'duration', value: event.eventDuration.toString()), DataGridCell<int>(columnName: 'invitees',
value: event.numberInvitees.toString()), DataGridCell<int>(columnName: ‘location',
value: event.location), ])); // To refresh the DataGrid based on CRUD
operation. notifyListeners(); } }
TextButton( onPressed: () { // You need to maintain GridSource as field gridSource.addDataGridRow( Event(‘Sample’, ‘9:00 AM’, '15 mins',’2’,’Sample’)); }, child: Text('Add new row')) |
If you still have doubt, can you share your simple project
to us? We will look and provide the solution ASAP.
Regards,
Neelakandan
|
void _addEvent() {
final Database database = Get.put(Database());
database.addEvent(Event(
eventName,
eventStart,
eventDuration,
attendees,
location,
));
gridSource.eventGrid.add(Event(
eventName,
eventStart,
eventDuration,
attendees,
location,
));
Get.back();
eventDuration = DateTime.now();
eventStart = DateTime.now();
gridSource.buildDataGridRows();
gridSource.updateDataGridSource();
}
|
|
class EventListScreen extends StatelessWidget {
GridSource dataSource;
final Database database = Get.put(Database());
@override
Widget build(BuildContext context) {
return GetBuilder<Database>(builder: (database) {
dataSource = GridSource(database.getEventList());
return Scaffold(
appBar: AppBar(
title: Text('Event Dashboard'),
backgroundColor: Colors.grey[850],
actions: [AddEventButton(source: dataSource)],
),
body: DataGrid(
source: dataSource,
));
});
}
} |
|
class EventListScreen extends StatelessWidget {
EventListScreen() {
database = Get.put(Database());
dataGridSource = GridSource(database: database);
}
Database database;
// Holds the data source for the DataGrid.
GridSource dataGridSource;
@override
Widget build(BuildContext context) {
return GetBuilder<Database>(
builder: (database) {
return Scaffold(
appBar: AppBar(
title: Text('Event Dashboard'),
backgroundColor: Colors.grey[850],
actions: [AddEventButton(source: dataGridSource)],
),
body: DataGrid(source: dataGridSource),
);
},
);
}
} |
|
final GridSource source;
@override
Widget build(BuildContext context) {
return Scaffold(
body: SafeArea(
child: GetBuilder<Database>(
builder: (database) {
return SfDataGrid(
source: source,
columnWidthMode: ColumnWidthMode.fill,
…… |
|
class GridSource extends DataGridSource {
GridSource({this.database}) {
// Build initial data grid rows.
initialLoading();
database.addListener(handleListUpdates);
print('Run');
}
Database database;
List<Event> eventList = [];
List<DataGridRow> _eventGridRow = [];
void initialLoading() {
eventList = database.getEventList();
buildDataGridRows();
}
// This will refresh the DataGrid when you add the new rows to DataBase.
void handleListUpdates() {
eventList = database.eventList;
buildDataGridRows();
notifyListeners();
}
………..
|