i have two columns "Debit" and "Credit". i want to caluclate total sum of Debit and Credit in Footer also i need to show (TotalDebit-TotalCredit) in next Row in footer for the referance iam attaching photo
|
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('DataGrid Table Summaries'),
),
body: SfDataGrid(
source: _employeeDataSource,
columnWidthMode: ColumnWidthMode.fill,
columns: getColumns,
tableSummaryRows: [
tableSummaryRow,
GridTableSummaryRow(
color: Colors.tealAccent,
showSummaryInRow: true,
columns: [],
position: GridTableSummaryRowPosition.bottom)
],
),
);
}
In DataGridSource:
@override
Widget? buildTableSummaryCellWidget(
GridTableSummaryRow summaryRow,
GridSummaryColumn? summaryColumn,
RowColumnIndex rowColumnIndex,
String summaryValue) {
if (summaryColumn == null) {
summaryValue =
'Total balance: ${double.parse(calculateSummaryValue(tableSummaryRow, debitSummaryColumn, debitRowColumnIndex)) - double.parse(calculateSummaryValue(tableSummaryRow, creditSummaryColumn, creditRowColumnIndex))} for the Date';
}
if (summaryColumn != null && summaryColumn.columnName == 'credit') {
creditRowColumnIndex = rowColumnIndex;
} else if (summaryColumn != null && summaryColumn.columnName == 'debit') {
debitRowColumnIndex = rowColumnIndex;
}
return Container(
padding: const EdgeInsets.all(15.0),
alignment: Alignment.center,
child: Text(summaryValue),
);
} |
i have int parse and its not work
help me
Hi Beknazar,
Based on the image you attached in this forum, we suspect that you have credit and debit properties as double. If it’s double, you get the summaryValue string in double format in the buildTableSummaryCellWidget i.e., string value format like "458600.0". So, you can't be able to convert the double string format directly into integer format. That’s why the exception occurred here. You can convert it to integer format once parsed into the double. Please check the following sample and code snippet.
|
@override Widget? buildTableSummaryCellWidget( GridTableSummaryRow summaryRow, GridSummaryColumn? summaryColumn, RowColumnIndex rowColumnIndex, String summaryValue) { if (summaryColumn == null) {
double debit = double.parse(calculateSummaryValue( tableSummaryRow, debitSummaryColumn, debitRowColumnIndex));
double credit = double.parse(calculateSummaryValue( tableSummaryRow, creditSummaryColumn, creditRowColumnIndex));
summaryValue = 'Total balance: ${debit.toInt() - credit.toInt()} for the Date'; } if (summaryColumn != null && summaryColumn.columnName == 'credit') { creditRowColumnIndex = rowColumnIndex; } else if (summaryColumn != null && summaryColumn.columnName == 'debit') { debitRowColumnIndex = rowColumnIndex; } return Container( padding: const EdgeInsets.all(15.0), alignment: Alignment.center, child: Text(summaryValue), ); }
class Employee { Employee(this.id, this.name, this.designation, this.debit, this.credit); final int id; final String name; final String designation; final double debit; final double credit; } |
Sample Link: https://www.syncfusion.com/downloads/support/directtrac/general/ze/sample1286343535
We hope this helps. Please let us know if you require any further assistance on this. We will be happy to assist you.
Regards,
Tamilarasan
what to do when there is decimal and int
how to do it right
help me
@ Tamilarasan Paranthaman help me
Hi beknazar,
By default, SfDataGrid provides support for calculating the sum of column summary values for the num data types, i.e., int and double. If you are using the Decimal type apart from the num type, the empty value will be displayed. So, you need to convert the Decimal type to a double or int type for calculating the summary value when mapping the DataGridRow. Please check the following sample and code snippet,
|
class Employee { Employee(this.id, this.name, this.designation, this.debit, this.credit); final int id; final String name; final String designation; final Decimal debit; final Decimal credit; }
List<Employee> getEmployeeData() { return [ Employee(10001, 'James', 'Project Lead', Decimal.parse('5000.00'), Decimal.parse('2000.0')), Employee(10002, 'Kathryn', 'Manager', Decimal.parse('8000.00'), Decimal.parse('6000.0')), Employee(10003, 'Lara', 'Developer', Decimal.parse('10000.00'), Decimal.parse('5000.0')), ]; }
In buildDataGridRow method:
class EmployeeDataSource extends DataGridSource { EmployeeDataSource( List<Employee> employees, this.debitSummaryColumn, this.creditSummaryColumn, this.tableSummaryRow, ) { buildDataGridRow(employees); }
…
void buildDataGridRow(List<Employee> employeeData) { dataGridRow = employeeData.map<DataGridRow>((employee) { return DataGridRow(cells: [ DataGridCell<int>(columnName: 'id', value: employee.id), DataGridCell<String>(columnName: 'name', value: employee.name), DataGridCell<String>( columnName: 'designation', value: employee.designation), DataGridCell<double>( columnName: 'debit', value: employee.debit.toDouble()), DataGridCell<double>( columnName: 'credit', value: employee.credit.toDouble()), ]); }).toList(); } … } |
Sample Link: https://www.syncfusion.com/downloads/support/directtrac/general/ze/main-957956191
We hope this helps. Please let us know if you need any further assistance with this.
Regards,
Tamilarasan
hi Tamilarasan. Thanks
i have one question. help me pleas.
Hi beknazar,
As per your requirement, you can achieve it in the DataGridSource.buildRow method. In the buildRow method, you can load the DropdownButton for the respective column. We have prepared a simple sample for that. In that sample, we have loaded the DropdownButton button for the city column. Please check the following sample and code snippet.
|
class EmployeeDataSource extends DataGridSource { EmployeeDataSource(this.employees) { buildDataGridRow(employees); }
…
/// Building a [DropDown] for combo box column. Widget buildDropDownWidget(String? displayText, List<String> dropDownMenuItems, DataGridRow dataGridRow) { return Container( padding: const EdgeInsets.symmetric(horizontal: 16.0), alignment: Alignment.centerLeft, child: DropdownButton<String>( value: displayText, autofocus: true, focusColor: Colors.transparent, underline: const SizedBox.shrink(), icon: const Icon(Icons.arrow_drop_down_sharp), isExpanded: true, style: textStyle, onChanged: (String? value) { final int dataRowIndex = dataGridRows.indexOf(dataGridRow);
if (value != null) { dataGridRows[dataRowIndex].getCells()[4] = DataGridCell<String>(columnName: 'city', value: value); employees[dataRowIndex].city = value; notifyListeners(); } }, items: dropDownMenuItems.map<DropdownMenuItem<String>>((String value) { return DropdownMenuItem<String>( value: value, child: Text(value), ); }).toList()), ); }
@override DataGridRowAdapter? buildRow(DataGridRow row) { return DataGridRowAdapter( cells: row.getCells().map<Widget>((dataGridCell) { return dataGridCell.columnName == 'city' ? buildDropDownWidget(dataGridCell.value, cities, row) : Container( alignment: Alignment.center, child: Text( dataGridCell.value.toString(), )); }).toList()); } } |
Sample Link: https://www.syncfusion.com/downloads/support/directtrac/general/ze/sample545048068
We hope this helps. Please let us know if you need any further assistance with this.
Regards,
Tamilarasan
thank you very much. Everything work