Delphi Pages Forums  

Go Back   Delphi Pages Forums > Delphi Forum > DB-Aware

Lost Password?

Reply
 
Thread Tools Display Modes
  #1  
Old 08-23-2001, 10:29 AM
tikiidol tikiidol is offline
Junior Member
 
Join Date: Jun 2001
Posts: 25
Default wwDBGRID and Sorting

is there any way to sort a wwdbgrid after the grid has been populated
i have seen examples where the data is coming directly from a table but i have not seen any where it is coming from a query.
the data is already ordered in a specific way and it constantly is changing so i wouldn't be able to include an ordered by statement in my sql. any suggestions?


thanks in advance for the help
Reply With Quote
  #2  
Old 08-23-2001, 10:55 AM
MrBaseball34 MrBaseball34 is offline
Senior Member
 
Join Date: Jan 2001
Posts: 7,260
Default RE: wwDBGRID and Sorting

Ok, here's how we did it in D2...

Include this unit ...

unit uSort;

interface

uses
Windows, Messages, SysUtils, Classes, Controls, Dialogs,
Menus, Grids, Wwdbigrd, Wwdbgrid, DB, DBTables, Wwquery,
Wwdatsrc, DBCtrls;

Type
TGridSort = Class(TObject)
Private
{Private Declarations}
Public
{Public Declarations}
procedure GridSort(Sender : TObject; Direction : String);
end;


implementation

procedure TGridSort.GridSort(Sender : TObject; Direction : String);
var
GridPos : Integer;
sList : TStringList;
sSql : String;
sStr : String;
strIndex,
strCount : Integer;
sSort : String;
sDataSet : TWWQuery;
OriginalSQL : String;

position : Integer;
intLength : Integer;
begin
GridPos := TwwDBGrid(Sender).GetActiveCol;
if ((TwwDBGrid(Sender).Fields[GridPos-1].DataType <> ftMemo) and
(TwwDBGrid(Sender).Fields[GridPos-1].DataType <> ftBlob) and
(TwwDBGrid(Sender).Fields[GridPos-1].DataType <> ftgraphic)) then
Begin
OriginalSQL := TwwQuery(TwwDBGrid(Sender).Datasource.Dataset).SQL .Text;
Position := Pos('order',OriginalSQL);
intLength := Length(OriginalSQL);
Delete(OriginalSQL,Position,(intLength-Position)-1);

if UpperCase(Direction) = 'ASCENDING' then
sSort := 'order by %s asc'
else if UpperCase(Direction) = 'DESCENDING' then
sSort := 'order by %s desc';

slist := TStringlist.create;
sDataSet := TwwQuery(TwwDBGrid(Sender).DataSource.DataSet);


sDataSet.GetFieldNames(sList);
sStr := sList.Strings[GridPos - 1];
sSql := sDataSet.SQL.Text;
strIndex := Pos(sStr, sSql);
if strIndex <> 0 then
begin //needed for select * type sql statements
StrCount := strIndex;
repeat Dec(strIndex) until sSQL[strIndex] = ' ';
strCount := (strCount + Length(sStr)) - StrIndex;
with sDataSet do begin
Close;
SQL.Text := originalSQL + Format(sSort,[Copy(sSQL,strIndex,StrCount)]);
Open;
end; //with q1
end // if..else statement
else begin
with sDataSet do begin
Close;
SQL.Text := originalSQL + Format(sSort,[sStr]);
Open;
end;
end;
sList.Free;
end
else
MessageDlg('Cannot sort on this field', mtinformation, [mbOK], 0);
end;

end.

and then call it like this:
{We had a Popup menu assigned to the wwDBGrid and menuitems
for sorting, Ascending, Descending, and Natural}

procedure TfrmAgentExplorer.pmnuSortAscClick(Sender: TObject);
begin
// comment
with TGridSort.Create do
begin
GridSort(pmnuMain.PopupComponent, 'ASCENDING');
Free;
end;
end;

procedure TfrmAgentExplorer.pmnuSortDescClick(Sender: TObject);
begin
// comment
with TGridSort.Create do
begin
GridSort(pmnuMain.PopupComponent, 'DESCENDING');
Free;
end;
end;

procedure TfrmAgentExplorer.pmnuSortNatClick(Sender: TObject);
begin
// comment
with TGridSort.Create do
begin
GridSort(pmnuMain.PopupComponent, 'NATURAL');
Free;
end;
end;

You can also do it in OnTitleButtonClick...

procedure TForm1.wwDBGrid1TitleButtonClick(Sender: TObject;
AFieldName: String);
begin
TwwDBGrid(Sender).SetActiveField(AFieldName);
with TGridSort.Create do
begin
GridSort(TwwDBGrid(Sender), 'DESCENDING');
Free;
end;
end;
Reply With Quote
  #3  
Old 08-23-2001, 12:10 PM
tikiidol tikiidol is offline
Junior Member
 
Join Date: Jun 2001
Posts: 25
Default RE: wwDBGRID and Sorting

Thanks for such a quick response
i am going to give it a try this is exactly what i was looking for
do i include this in my actual application or as a part of the actual component source?
also my column headers have display titles ..will that interfere with the process? and i used a tquery rather than a twwquery will that cause a problem?
thanks again :-)
Reply With Quote
  #4  
Old 08-23-2001, 01:25 PM
MrBaseball34 MrBaseball34 is offline
Senior Member
 
Join Date: Jan 2001
Posts: 7,260
Default RE: wwDBGRID and Sorting

Yes, add the unit to your project and use it in the units
where your grid lies. Column Titles will not interfere
with the operation as it uses either the fieldname, in the
TitleButtonClick, or the activecolumn in the menu code.

Just change te twwquery stuff to tquery. If I were you, I'd
move everything to twwquery, anyway...
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 Off

Forum Jump


All times are GMT. The time now is 11:47 AM.


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