0
votes

how to make button on delphi 7 to execute addition with all data in dbgrid delphi. for example i have database table with 3 coloumns show in dbgrid,

[CODE_NUMBER][ITEMS NAME][STOCK][NEW_STOCK]
 001          Rackets       1         5
 002          Sports Shoes  2         5
 003          Golf Hat      3         5 
 ... etc

How to create button when i click it, then dbgrid start addition

[STOCK] = [STOCK]+[NEW_STOCK]

after count in the first line, move to second line do the same addition and so on until the end of the record in dbgrid and delete data in [NEW_STOCK] coloumn. i've try with

if dbgrid1.fieldbyname('Code').value <> 0 then
begin
dbgrid1.fieldbyname('Stock').value := dbgrid1.fieldbyname('Stock').value + dbgrid1.fieldbyname('NEW_STOCK').value;
dbgrid1.next;

but only affect in the first line, nothing happen with the next lines in dbgrid

2
It is not 100% clear to me what you are trying to achieve, but i think perhaps what you need is a calculated field. The calculated field is defined in the dataset and allows you to define the calculation in Delphi code. Look at OnCalcFields in the help.Hugh Jones
thanks for your attention David,Arie Prasetyo
@Hugh Jones, Thanks for your attention, my problem is solved,Arie Prasetyo

2 Answers

0
votes

To change data in dbgrid, you should use it's corresponding dataset, i.e.:

with dbgrid1.DataSource.DataSet do begin
  Edit;
  Fields.fieldbyname('Stock').value := Fields.fieldbyname('Stock').value + Fields.fieldbyname('NEW_STOCK').value;
  Post;
  Next;
end;
0
votes

More efficient would be to do this at the database

The update statement to send to the database is very simple :

update yourtable set Stock = Stock + newStockValue where Code <> 0

and then just refresh your query or table component.