Components for Delphi and C++ Builder.

Go to Russian forum
Go to EhLib.com
It is currently 19 Apr 2024, 20:18

All times are UTC




Post new topic Reply to topic  [ 7 posts ] 
Author Message
PostPosted: 30 Apr 2020, 17:01 
Offline

Joined: 24 Aug 2018, 13:15
Posts: 46
Location: Brazil
Hello,


I would like to know if it is possible to update the sum of the footer when selecting specific lines in the grid

Here is an example video



https://drive.google.com/open?id=1T7k1RcPlhYlDCrJ5vn2nBGLL8Vdum5-i


Top
 Profile  
 
PostPosted: 01 May 2020, 12:06 
Offline

Joined: 08 May 2014, 18:06
Posts: 663
Hello

Yes, it is possible.

Unfortunately, the grid cannot automatically calculate the amounts only for the selected record, but you can implement this possibility using the TDBGridEh.OnSelectionChanged event
and calculate the amount yourself.

Here is a code and Demo project.

Code:
procedure TForm1.DBGridEh1SelectionChanged(Sender: TObject);
begin
  if (DBGridEh1.SelectedRows.Count = 0) then
  begin
    DBGridEh1.FieldColumns['Area'].Footer.ValueType := fvtSum;
  end else
  begin
    DBGridEh1.FieldColumns['Area'].Footer.ValueType := fvtStaticText;
    DBGridEh1.FieldColumns['Area'].Footer.Value := CalcSelectedRows.ToString;
  end;
end;

function TForm1.CalcSelectedRows: Double;
var
  I: Integer;
  MemRec: TMemoryRecordEh;
begin
  Result := 0;
  for I := 0 to DBGridEh1.SelectedRows.Count - 1 do
  begin
    MemRec := MemTableEh1.BookmarkToRec(DBGridEh1.SelectedRows[i]);
    MemTableEh1.InstantReadEnter(MemRec, -1);
    Result := Result + MemTableEh1.FieldByName('Area').AsFloat;
    MemTableEh1.InstantReadLeave;
  end;
end;


Attachments:
DBGridEh-SumOfSelectedRows.zip [5.16 KiB]
Downloaded 137 times
sshot-95.png
sshot-95.png [ 14.28 KiB | Viewed 2275 times ]

_________________
Best regards
EhLib Support Team
Top
 Profile  
 
PostPosted: 04 May 2020, 17:08 
Offline

Joined: 24 Aug 2018, 13:15
Posts: 46
Location: Brazil
It worked :D :D

But I had to make some changes using Bookmark directly on TClientDataSet.GotoBoomark, because in my project I can't use MemTableEh natively.

So I created a MemTableEh and a TDataSetDriverEh at runtime

But when I use MemTable data, an access violation occurs.
Simulation example below:
Select some record and press the button
https://drive.google.com/open?id=1cWLhK ... qAksSdOlC8

Another situation, i have created the footer at run time, when this occurs the DisplayFormat property of the TColumnFooterEh class does not work, only if the footer already exists at the Collection.


Top
 Profile  
 
PostPosted: 05 May 2020, 15:14 
Offline

Joined: 08 May 2014, 18:06
Posts: 663
Hello

I cannot understand what you want to show with your example.

Quote:
But I had to make some changes using Bookmark directly on TClientDataSet.GotoBoomark, because in my project I can't use MemTableEh natively.
Can you rephrase this statement?

I showed you an example of displaying the sum of selected records using an example of using a DataSet of TMemTableEh type.
Do you need an example using an DataSet of TClientDataSet type?

_________________
Best regards
EhLib Support Team


Top
 Profile  
 
PostPosted: 05 May 2020, 16:51 
Offline

Joined: 24 Aug 2018, 13:15
Posts: 46
Location: Brazil
EhLibSupport wrote:
Hello

I cannot understand what you want to show with your example.

Quote:
But I had to make some changes using Bookmark directly on TClientDataSet.GotoBoomark, because in my project I can't use MemTableEh natively.
Can you rephrase this statement?


Quote:
I showed you an example of displaying the sum of selected records using an example of using a DataSet of TMemTableEh type.
Do you need an example using an DataSet of TClientDataSet type?



With the example I sent, I wanted to show that if you create a MemTableEh at run time, an access violation is occurring. The TClientDataSet I needed to use when I got to this problem with TMemTableEh.
Everything was fine, but I found a "possible" bug that I added in the example above.

Another situation I encountered, when I create a footer for the column at run time, DisplayFormat does not work.


Top
 Profile  
 
PostPosted: 05 May 2020, 17:55 
Offline

Joined: 08 May 2014, 18:06
Posts: 663
Hello

Code:
procedure TForm67.Button1Click(Sender: TObject);
var
  Mem: TMemTableEh;
  Dat: TDataSetDriverEh;
  MemRec: TMemoryRecordEh;
  I:Integer;
begin
  Mem := TMemTableEh.Create(Self);
  Dat := TDataSetDriverEh.Create(Self);
  try
    Dat.ProviderDataSet := DBGridEh1.DataSource.DataSet;
    Mem.DataDriver := Dat;
    Mem.Close;
    Mem.Open;
    for I := 0 to DBGridEh1.SelectedRows.Count - 1 do
    begin
      MemRec := Mem.BookmarkToRec(DBGridEh1.SelectedRows[I]);
      Mem.InstantReadEnter(MemRec, -1);
      ShowMEssage(Mem.FieldByName('ESTABELEC').AsString);
      Mem.InstantReadLeave;
    end;
  finally
    FreeAndNil(Mem);
    FreeAndNil(Dat);
  end;
end;


Your example is incorrect in principle.
If DBGridEh is connected to TClientDataSet then you should apply DBGridEh1.SelectedRows[I] only to TClientDataSet.
Like this

Code:
    for I := 0 to DBGridEh1.SelectedRows.Count - 1 do
    begin
//      MemRec := Mem.BookmarkToRec(DBGridEh1.SelectedRows[I]);
//      Mem.InstantReadEnter(MemRec, -1);
//      ShowMEssage(Mem.FieldByName('ESTABELEC').AsString);
//      Mem.InstantReadLeave;
        cds.GotoBookmark(DBGridEh1.SelectedRows[I]);
        ShowMEssage(cds.FieldByName('ESTABELEC').AsString);
    end;


You don't need TMemTableEh here at all.

_________________
Best regards
EhLib Support Team


Top
 Profile  
 
PostPosted: 05 May 2020, 19:22 
Offline

Joined: 24 Aug 2018, 13:15
Posts: 46
Location: Brazil
That's what I did.


Thank you for your help :)


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 7 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 6 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Group