Board index » delphi » Sorting dbgrid by clicking on title

Sorting dbgrid by clicking on title


2003-10-10 05:37:25 AM
delphi116
Hi, I have a table with 3 fields and one index of each one.
Then I have made a dbgrid with 3 columns with each column caption's name
same as index's name.
Can I sort the dbgrid changing the order by choosing any column? Whichs
the right command?
I've tried a lot, but no one seems to work.
For ex. I have tried table.indexname := column.field.fieldname, it compiles
but does not run
Thanks
Leonardo
Montevideo-URUGUAY
--
Using M2, Opera's revolutionary e-mail client: www.opera.com/m2/
 
 

Re:Sorting dbgrid by clicking on title

If you use ADO ... this is what I sometimes do...
procedure Tform1.Grid1TitleClick(Column: TColumn);
begin
if ADODataset1.IndexFieldNames = column.FieldName then begin
ADODataset1.IndexFieldNames:=column.FieldName + ' DESC';
end else begin
ADODataset1.IndexFieldNames:=column.FieldName;
end;
end;
"Leonardo Bidi" <XXXX@XXXXX.COM>writes
Quote

Hi, I have a table with 3 fields and one index of each one.
Then I have made a dbgrid with 3 columns with each column caption's name
same as index's name.
Can I sort the dbgrid changing the order by choosing any column? Whichs
the right command?
I've tried a lot, but no one seems to work.
For ex. I have tried table.indexname := column.field.fieldname, it compiles
but does not run

Thanks
Leonardo
Montevideo-URUGUAY

--
Using M2, Opera's revolutionary e-mail client: www.opera.com/m2/
 

Re:Sorting dbgrid by clicking on title

In article <XXXX@XXXXX.COM>, XXXX@XXXXX.COM
says...
Quote

Hi, I have a table with 3 fields and one index of each one.
Then I have made a dbgrid with 3 columns with each column caption's name
same as index's name.
Can I sort the dbgrid changing the order by choosing any column? Whichs
the right command?
I've tried a lot, but no one seems to work.
For ex. I have tried table.indexname := column.field.fieldname, it compiles
but does not run
If you use "order by" queries, it is MUCH easier...
This was originally written to use wwDBGrid and TwwQuery
but I just modified to work with TQuery and TDBGrid as well
as called from TDBGrid.OnTitleClick:
This IS TESTED!
In your DBGrid's OnTitleClick:
procedure TForm1.DBGrid1TitleClick(Column: TColumn);
begin
with TGridSort.Create do
begin
// First param is the column that was clicked on,
// second, sort order
GridSort(Column, 'ASCENDING');
Free;
end;
end;
Now for the uSort Unit:
unit usort;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms,
Dialogs, Menus, Grids, DBGrids, DB, DBTables, ExtCtrls, DBCtrls,
StdCtrls;
Type
TGridSort = Class(TObject)
Private
{Private Declarations}
Public
{Public Declarations}
procedure GridSort(Sender : TColumn; Direction : String);
end;
implementation
procedure TGridSort.GridSort(Sender: TColumn; Direction : String);
var
GridPos : integer;
sList : TStringList;
sSql : String;
sStr : String;
strIndex,
strCount : integer;
sSort : String;
sDataSet : TQuery;
OriginalSQL : String;
position : integer;
intLength : Integer;
begin
GridPos := TColumn(Sender).Index; // Get the active column for this
grid
if ((TColumn(Sender).Field.dataType <>ftMemo) and
(TColumn(Sender).Field.dataType <>ftBlob) and
(TColumn(Sender).Field.dataType <>ftgraphic)) then
Begin
OriginalSQL := TQuery(TColumn
(sender).Grid.Datasource.Dataset).SQL.Text;
{Pos(Substr: string; S: string): Integer;}
Position := pos('order',OriginalSQL);
intLength := Length(OriginalSQL);
{Delete(var S: string; Index, Count:Integer);}
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 := TQuery(TColumn(sender).Grid.Datasource.Dataset);
sDataSet.Getfieldnames(sList);
sStr := sList.strings[GridPos];
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 sDataSet
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.