简体   繁体   中英

How to create button to addition every lines in DBGrid Delphi 7

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

To change data in dbgrid, you should use it's corresponding dataset, ie:

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM