


Lucky you if you're using a dataset with aggregation capabilities (like TClientDataSet), because you can just declare what you want in a TAggregateField and forget about doing calculations "by hand". Looks like not even you know where it happens and where it doesn't happen.Ĭonsider using the Field's events (for example, OnChange event) to perform that kind of calculations. I think you're mixing a business logic (like calculating a total) with User Interaction logic (like the event on which some grid column loses the focus) and that's the source of the erratic behavior of your application. All calculated fields will then be displayed in the grid and each row will show the correct calculated value based on the values for that row. It's best to simply add them as calculated fields to the dataset and then link the grid to the dataset. Torcamen_SUB.FieldByName('Precovenda').AsFloat + (Torcamen_SUB.FieldByName('Precovenda').AsFloat * Torcamen_SUB.FieldByName('Desconto').AsFloat)/100) Ī general rule of thumb is that calculated values shouldn't be saved to the database unless really really necessary. Torcamen_SUB.FieldByName('Total').AsFloat := Torcamen_SUB.FieldByName('QT').AsFloat * (

#Delphi dbgrid export to pdf code
Click Ok and then add code similar to this for the OnCalcField event of the table: Simply add an extra field using the field editor of the table, type in the field name Total, select the correct datatype and then select the field type as Calculated. The Total field shouldn't actually be a field in the database since but rather a calculated field based on values from other fields. The better way to implement calculations is actually to move the calculation to your TTable component that the grid is linked to.
