Delphi Pages Forums  

Go Back   Delphi Pages Forums > Delphi Forum > General

Lost Password?

Reply
 
Thread Tools Display Modes
  #1  
Old 09-27-2016, 04:35 AM
rmt rmt is offline
Senior Member
 
Join Date: Jun 2013
Posts: 299
Default Add Sum of a Column in TDBGRID in Delphi5

Dear Expert,

I want to add a Sum (Total) of a Column in TDBGrid in Delphi5 application. It would have been easier to add Sum of a Column had i used TDxDBGrid but I am using TDBGrid. Please suggest how shall i do it.

Thanks in advance.

Regards
__________________
Rmt
Reply With Quote
  #2  
Old 09-27-2016, 06:36 AM
Jasser Jasser is offline
Moderator
 
Join Date: Jan 2005
Location: Saudi Arabia
Posts: 5,059
Default

Is this going to editable grid or read only grid?
__________________
Regards,
Abdulaziz Jasser
Reply With Quote
  #3  
Old 09-27-2016, 07:01 AM
rmt rmt is offline
Senior Member
 
Join Date: Jun 2013
Posts: 299
Default

It is Editable grid but the column with Sum is Read only.
__________________
Rmt
Reply With Quote
  #4  
Old 09-27-2016, 08:33 AM
Jasser Jasser is offline
Moderator
 
Join Date: Jan 2005
Location: Saudi Arabia
Posts: 5,059
Default

Ok..

What is the type of the DataSet connected to the grid (Table/Query or memory table...etc) please give full details?
__________________
Regards,
Abdulaziz Jasser
Reply With Quote
  #5  
Old 09-27-2016, 08:54 AM
Jasser Jasser is offline
Moderator
 
Join Date: Jan 2005
Location: Saudi Arabia
Posts: 5,059
Default

I assume whenever the data in the dataset is added/updated/deleted the total of the column will change. Therefore, it can be done in different ways depending on the type of the Dataset.

In my case I use ElavateDB memory table as a Dataset and use "AfterPost" event to run a small query to get the total of the column and put it at the bottom of the column using TLabel.
__________________
Regards,
Abdulaziz Jasser
Reply With Quote
  #6  
Old 09-27-2016, 03:33 PM
rmt rmt is offline
Senior Member
 
Join Date: Jun 2013
Posts: 299
Default

I'm using TADODataset
__________________
Rmt
Reply With Quote
  #7  
Old 09-28-2016, 08:50 AM
Jasser Jasser is offline
Moderator
 
Join Date: Jan 2005
Location: Saudi Arabia
Posts: 5,059
Default

Then you have to loop the dataset to get the total (It's not as good as using SQL). You can reach that using "AfterPost" event. Do the following:

1- Take a Bookmark of the record.
2- Go to the first record.
3- Take the value of the field and keep looping to the next records.
4- Go back to the bookmark.
5- Show the total in maybe a label.

Something like this:

Code:
procedure TForm9.ADODataSet1AfterPost(DataSet: TDataSet);
var
          xSum : Integer;
          pBM  : TBookmark;
begin
          xSum := 0;

          pBM := ADODataSet1.GetBookmark;

          ADODataSet1.DisableControls;
          ADODataSet1.First;

          while NOT ADODataSet1.Eof do begin
                xSum := xSum + ADODataSet1['Thefield'];

                ADODataSet1.Next;
          end;

          ADODataSet1.GotoBookmark(pBM);
          ADODataSet1.EnableControls;

          Label1.Caption := IntToStr(xSum);
__________________
Regards,
Abdulaziz Jasser
Reply With Quote
  #8  
Old 09-28-2016, 09:41 AM
rmt rmt is offline
Senior Member
 
Join Date: Jun 2013
Posts: 299
Default

Thanks will try this option.
__________________
Rmt
Reply With Quote
  #9  
Old 10-01-2016, 07:04 PM
tcsh tcsh is offline
Member
 
Join Date: Sep 2016
Posts: 37
Default

Quote:
Originally Posted by Jasser View Post
I assume whenever the data in the dataset is added/updated/deleted the total of the column will change. Therefore, it can be done in different ways depending on the type of the Dataset.
Hello,

Can you give me the same info but with my dataset?

I'm using DBGrid, I added a Data Module unit and on it I have the following:

TSimpleDataSet
TDataSource
TSQLQuery
TSQLConnection

These are the components I'm using to pull data from the db to the TDBGrid and various other things I've included (add, edit and delete customer buttons)

I'd like to add a TLabel? or .. TDBText? (which is better?) under the TDBGrid, specifically under the Price column (my db is among other things has a price column where there are sums, product prices basically) which will get the price value entered in each row for that column and return a total sum.
It should also update once a new value is added, like if I'm browsing the grid and I choose to add a new customer with all the necessary info, including price, that would obviously add a new price to add to the total sum.

Would appreciate it if you can help, thanks!
Reply With Quote
  #10  
Old 10-02-2016, 07:26 AM
Jasser Jasser is offline
Moderator
 
Join Date: Jan 2005
Location: Saudi Arabia
Posts: 5,059
Default

Use the same code that I posted before. On the other hand, you can use a TLabel or TEdit to show the sum. I prefer to use a TLabe.
__________________
Regards,
Abdulaziz Jasser
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On

Forum Jump


All times are GMT. The time now is 02:31 AM.


Powered by vBulletin® Version 3.8.8
Copyright ©2000 - 2019, vBulletin Solutions, Inc.