View Single Post
  #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