Hi
I have used the code below to allow sorting a stringgrid by clicking on the
column header. Clicking again alternates between ascending and descending.
The current sorted column is stored in the tag field of the stringgrid. I
have deleted a few lines that are not required but should compile as below.
Warren


procedure TForm1.StringGrid1(Sender: TObject;
  Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
var
  r,SortCol : Integer;
begin
  with TStringGrid(Sender) do begin
    MouseToCell(x,y,SortCol,r);
    if r=0 then begin
      if (SortCol+1)=Tag then
        Tag:=-(SortCol+1)
      else
        Tag:=(SortCol+1);
      SortStringGrid(Sender);
    end;
  end;
end;


procedure SortStringGrid(Sender: TObject);
var
  r,c,SortCol,rc,cc : Integer;
  sl : TStringList;
  s : array of array of String;
begin
  with TStringGrid(Sender) do
    if Tag<>0 then begin
      // ignore blank lines
      rc:=RowCount;
      cc:=ColCount;
      while (rc>1) and (Cells[0,rc-1]='') do
        Dec(rc);
      SortCol:=Abs(Tag)-1;
      SetLength(s,cc,rc);
      SetLength(b,rc);
      // copy cells to sort to stringlist
      sl:=TStringList.Create;
      for r:=1 to rc-1 do
        sl.AddObject(UpperCase(Cells[SortCol,r]+' '+Cells[0,r]),TObject(r));
      sl.Sort;
      // copy stringgrid data to temporary array
      for r:=1 to rc-1 do
        for c:=0 to cc-1 do
          s[c,r]:=Cells[c,r];
      // copy data back in sorted order
      if Tag<0 then begin
        for r:=1 to rc-1 do
          for c:=0 to cc-1 do
            Cells[c,rc-r]:=s[c,Integer(sl.Objects[r-1])];
      end
      else begin
        for r:=1 to rc-1 do
          for c:=0 to cc-1 do
            Cells[c,r]:=s[c,Integer(sl.Objects[r-1])];
      end;
      sl.Free;
    end;
end;


-----Original Message-----
From: Chris Veale [mailto:[EMAIL PROTECTED]
Sent: Thursday, 24 July 2003 02:43 p.m.
To: Multiple recipients of list delphi
Subject: [DUG]: tstringgrid sorting.


Hi all.

I want to sort a tstringgrid in ascending order of one column but want to
also order the rest of the columns also (change the order of the rows)

Is there a function or something to do this or will I have to write a custom
sort tool myself?

Id prefer something like the tlistbox sort procedure but the difficulty
comes when I want to organise the remainder of the columns also.

Cheers

Chris Veale

---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.502 / Virus Database: 300 - Release Date: 18/07/03




______________________________________________________
The contents of this e-mail are privileged and/or confidential to the
named recipient and are not to be used by any other person and/or
organisation. If you have received this e-mail in error, please notify 
the sender and delete all material pertaining to this e-mail.
______________________________________________________
---------------------------------------------------------------------------
    New Zealand Delphi Users group - Delphi List - [EMAIL PROTECTED]
                  Website: http://www.delphi.org.nz
To UnSub, send email to: [EMAIL PROTECTED] 
with body of "unsubscribe delphi"
Web Archive at: http://www.mail-archive.com/delphi%40delphi.org.nz/

#####################################################################################
This e-mail message has been scanned for Viruses and Content and cleared 
by MailMarshal
For more information please visit www.marshalsoftware.com
#####################################################################################
---------------------------------------------------------------------------
    New Zealand Delphi Users group - Delphi List - [EMAIL PROTECTED]
                  Website: http://www.delphi.org.nz
To UnSub, send email to: [EMAIL PROTECTED] 
with body of "unsubscribe delphi"
Web Archive at: http://www.mail-archive.com/delphi%40delphi.org.nz/

Reply via email to