<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=us-ascii">
<TITLE>Message</TITLE>
<META content="MSHTML 6.00.5730.11" name=GENERATOR></HEAD>
<BODY>
<DIV><FONT face=Arial color=#0000ff size=2><SPAN class=495331623-01022007>Here
is the stringgrid sort routine - I use the stock standard VCL stringgrid in many
places and sort it using this.</SPAN></FONT></DIV>
<DIV><FONT face=Arial color=#0000ff size=2><SPAN
class=495331623-01022007></SPAN></FONT> </DIV>
<DIV><FONT face=Arial color=#0000ff size=2><SPAN class=495331623-01022007>I
usually add a row of buttons above the grid which the user can click to sort the
column it is above, and no fixed rows, so it looks similar to the Windows
Explorer sort options.</SPAN></FONT></DIV>
<DIV><FONT face=Arial color=#0000ff size=2><SPAN
class=495331623-01022007></SPAN></FONT> </DIV>
<DIV><FONT face=Arial color=#0000ff size=2><SPAN class=495331623-01022007>The
onclick event for each button just calls the relevant sort:</SPAN></FONT></DIV>
<DIV><FONT face=Arial color=#0000ff size=2><SPAN
class=495331623-01022007></SPAN></FONT> </DIV>
<DIV><FONT face=Arial color=#0000ff size=2><SPAN
class=495331623-01022007>eg</SPAN></FONT></DIV>
<DIV><FONT face=Arial color=#0000ff size=2><SPAN
class=495331623-01022007></SPAN></FONT> </DIV>
<DIV><FONT face=Arial color=#0000ff size=2><SPAN
class=495331623-01022007>procedure Tform1.SortGridbyName;<BR>begin<BR>
xcSortGrid(strgrdSearch,3,4,'A',true);<BR>
strgrdSearch.setfocus;<BR>end;<BR></SPAN></FONT></DIV>
<DIV><FONT face=Arial color=#0000ff size=2><SPAN class=495331623-01022007>
<DIV><FONT face=Arial color=#0000ff size=2><SPAN class=495331623-01022007>The
sorting is a Tstringlist sort, that is alpha, but there is facility in the
routine to specify other column types eg Integer or Date.</SPAN></FONT></DIV>
<DIV><FONT face=Arial color=#0000ff size=2><SPAN
class=495331623-01022007>(Internally the routine formats these into a
string so the alpha sort works - se the jkblSortGridKeyType1
etc)</SPAN></FONT></DIV>
<DIV><FONT face=Arial color=#0000ff size=2><SPAN
class=495331623-01022007></SPAN></FONT> </DIV></DIV></SPAN></FONT>
<DIV><FONT face=Arial color=#0000ff size=2></FONT> </DIV>
<DIV><FONT face=Arial color=#0000ff size=2>procedure xcSortGrid(var
PGrid:TStringGrid;SortColumn1:Integer;SortColumn2:integer;AscDesc:char;CaseInSensitive:Boolean);<BR>//NOTE
there are extra sort types poss<SPAN class=495331623-01022007>ible</SPAN>,
defined in
jkblSortGridKeyType1&2<BR>var<BR>TmpSortList:TStringList;<BR>olddefrow,newdefrow:integer; //keep
same data row as
default<BR>oldrow,datalhs,ptr,colptr,rowptr:integer;<BR>keydata1,keydata2,tempstr:string;<BR>firstrow:integer;
//first row after fixed rows<BR>lAscDesc:char;<BR>tempcells:array of array of
string;<BR>begin<BR> if PGrid.rowcount < 2 then exit; //nothing to
sort<BR> TmpSortList:=TStringList.Create;<BR>
TmpSortList.sorted:=false;<BR> olddefrow:=-1;<BR>
NewDefRow:=-1;<BR> if PGrid.row>0 then oldDefRow:=PGrid.row;<BR>
firstrow:=0;<BR> if PGrid.FixedRows >0 then<BR>
firstrow:=PGrid.FixedRows; //1 fixed row-> 1=1st data
row<BR> for ptr:=firstrow to PGrid.rowcount-1 do<BR>
begin<BR> //put sort col plus orig pointer into
stringlist<BR>
keydata1:=PGrid.cells[SortColumn1,ptr];<BR>
keydata2:=PGrid.cells[SortColumn2,ptr];<BR> //this can be
extended to specify decimal columns<SPAN class=495331623-01022007>/Date</SPAN> -
ideally need more data checking though<BR> //if
jkblSortGridKeyType1='I' then
keydata1:=format('%12d',[strtoint(keydata1)]);<BR> //if
jkblSortGridKeyType2='I' then
keydata2:=format('%12d',[strtoint(keydata<SPAN
class=495331623-01022007>2</SPAN>)]);</FONT></DIV>
<DIV><FONT face=Arial color=#0000ff size=2></FONT> </DIV>
<DIV><FONT face=Arial color=#0000ff size=2>
tempstr:=keydata1+keydata2;<BR> if CaseInsensitive=true then
uppercase(tempstr);<BR> xcstrpad(tempstr,60);<SPAN
class=495331623-01022007> //see below</SPAN><BR>
tempstr:=tempstr+inttostr(ptr);<BR>
TmpSortList.Add(tempstr);<BR> end;<BR> //sort it<BR>
TmpSortList.sorted:=true;<BR> //now copy original cells to tempcells
dynamic array<BR> //this is to keep original data as grid is now being
overwritten<BR>
setlength(tempcells,PGrid.colcount,PGrid.rowcount);<BR> for
rowptr:=firstrow to PGrid.rowcount-1 do<BR> begin<BR>
for colptr:=0 to PGrid.colcount-1 do<BR>
begin<BR>
tempcells[colptr,rowptr]:=PGrid.cells[colptr,rowptr];<BR>
end;<BR> end;<BR> //now write tempcells back to grid - note grid may
have more than sorted list if<BR> //fixed rows, so 2 counters, ptr (list)
and rowptr(grid)<BR> lAscDesc:=upcase(AscDesc);<BR> if lAscDesc='A'
then<BR> begin<BR> for ptr:=0 to TmpSortList.Count-1
do<BR> begin<BR>
tempstr:=copy(TmpSortList[ptr],61,10);<BR>
oldrow:=strtoint(tempstr);<BR> if oldrow=oldDefRow
then NewDefRow:=ptr;<BR> //oldptr is orig row
number, ptr is new row number<BR>
rowptr:=ptr+firstrow;<BR> for colptr:=0 to
PGrid.colcount-1 do<BR>
begin<BR>
PGrid.cells[colptr,rowptr]:=tempcells[colptr,oldrow];<BR>
end;<BR> end;<BR> end<BR> else<BR>
begin<BR> for ptr:=TmpSortList.count-1 downto 0
do<BR> begin<BR>
tempstr:=copy(TmpSortList[ptr],61,10);<BR>
oldrow:=strtoint(tempstr);<BR> if oldrow=oldDefRow
then NewDefRow:=ptr;<BR> //oldptr is orig row
number, ptr is new row number<BR>
rowptr:=ptr+firstrow;<BR> for colptr:=0 to
PGrid.colcount-1 do<BR>
begin<BR>
PGrid.cells[colptr,rowptr]:=tempcells[colptr,oldrow];<BR>
end;<BR> end;<BR> end;<BR>
TmpSortList.free;<BR> tempcells:=nil;<BR> if (OldDefRow>0) and
(NewDefRow > FirstRow) then PGrid.row:=NewDefRow;<BR> //in case row not
set, makes sure is valid...otherwise grid can sort of scroll<BR> //or
duplicate fixed row...<BR> if (PGrid.Row<firstrow) or (PGrid.Row >
PGrid.RowCount-1) then PGrid.Row:=firstrow+1;<BR>
jkblSortGridKeyType1:='';<BR>
jkblSortGridKeyType2:='';<BR>end;<BR></FONT></DIV>
<DIV><FONT face=Arial color=#0000ff size=2>procedure xcstrpad(var
strec:string;stlen:integer);<BR>var<BR>Rsize,I:integer;<BR>begin<BR>
Rsize:=length(strec);<BR> if Rsize < Stlen then<BR>
for i:=Rsize+1 to Stlen do Strec:=Strec+<SPAN class=495331623-01022007>'
'</SPAN>;<BR>end;<BR></FONT></DIV>
<DIV><FONT face=Arial color=#0000ff size=2></FONT> </DIV>
<DIV><FONT face=Arial color=#0000ff size=2><SPAN
class=495331623-01022007>Note:</SPAN></FONT></DIV>
<DIV><FONT face=Arial color=#0000ff size=2><SPAN class=495331623-01022007>These
are global variables, if wanted (remove them if not needed) to further extend
the sort column types.</SPAN></FONT></DIV>
<DIV><FONT face=Arial color=#0000ff size=2><SPAN class=495331623-01022007>I
added these later to extend the possible sort types.</SPAN></FONT></DIV>
<DIV><FONT face=Arial color=#0000ff size=2><SPAN
class=495331623-01022007></SPAN></FONT> </DIV>
<DIV><FONT face=Arial color=#0000ff
size=2>jkblSortGridKeyType1,jkblSortGridKeyType2:string; //for more sort
types<BR></FONT></DIV>
<DIV><FONT face=Arial color=#0000ff size=2></FONT> </DIV>
<DIV class=Section1>
<P class=MsoAutoSig align=left>John</P>
<P class=MsoAutoSig align=left><?xml:namespace prefix = o ns =
"urn:schemas-microsoft-com:office:office" /><o:p><FONT face=Arial size=2>Web <A
href="http://www.jbcl.co.nz/">www.jbcl.co.nz</A></FONT></o:p></P>
<P class=MsoAutoSig><o:p><FONT face=Arial size=2></FONT></o:p> </P>
<P class=MsoAutoSig><o:p><!--StartFragment --> </o:p></P></DIV>
<BLOCKQUOTE style="MARGIN-RIGHT: 0px">
<DIV></DIV>
<DIV class=OutlookMessageHeader lang=en-us dir=ltr align=left><FONT
face=Tahoma size=2>-----Original Message-----<BR><B>From:</B>
delphi-bounces@ns3.123.co.nz [mailto:delphi-bounces@ns3.123.co.nz] <B>On
Behalf Of </B>Eion McIntosh (Christchurch)<BR><B>Sent:</B> Friday, 2 February
2007 9:07 a.m.<BR><B>To:</B> NZ Borland Developers Group - Delphi
List<BR><B>Subject:</B> [DUG] Stringgrids<BR><BR></FONT></DIV><!-- Converted from text/rtf format -->
<P><FONT face=Arial size=2>Hi</FONT> </P>
<P><FONT face=Arial size=2>I havd a look, but can't see anything, but can you
take 2 rows within a TStringGrid and swap them? I have some details in a
stringgrid but would like to sort it on one of the rows after it has been
filled. I can't fill it in order to start with because the source data is
coming from two different sources.</FONT></P>
<P><FONT face=Arial size=2>Regards</FONT> <BR><FONT face=Arial size=2>Eion
McIntosh</FONT> <BR><FONT face=Arial size=2>PPCS Ltd</FONT>
</P><BR><BR>__________ NOD32 1.1461 (20060329) Information
__________<BR><BR>This message was checked by NOD32 antivirus system.<BR><A
href="http://www.eset.com">http://www.eset.com</A><BR></BLOCKQUOTE></BODY></HTML>