<!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>&nbsp;</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>&nbsp;</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>&nbsp;</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>&nbsp;</DIV>
<DIV><FONT face=Arial color=#0000ff size=2><SPAN 
class=495331623-01022007>procedure Tform1.SortGridbyName;<BR>begin<BR>&nbsp; 
xcSortGrid(strgrdSearch,3,4,'A',true);<BR>&nbsp; 
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,&nbsp; 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&nbsp;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>&nbsp;</DIV></DIV></SPAN></FONT>
<DIV><FONT face=Arial color=#0000ff size=2></FONT>&nbsp;</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&amp;2<BR>var<BR>TmpSortList:TStringList;<BR>olddefrow,newdefrow:integer;&nbsp;//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>&nbsp; if PGrid.rowcount &lt; 2 then exit;&nbsp;//nothing to 
sort<BR>&nbsp; TmpSortList:=TStringList.Create;<BR>&nbsp; 
TmpSortList.sorted:=false;<BR>&nbsp; olddefrow:=-1;<BR>&nbsp; 
NewDefRow:=-1;<BR>&nbsp; if PGrid.row&gt;0 then oldDefRow:=PGrid.row;<BR>&nbsp; 
firstrow:=0;<BR>&nbsp; if PGrid.FixedRows &gt;0 then<BR>&nbsp;&nbsp;&nbsp; 
firstrow:=PGrid.FixedRows;&nbsp;&nbsp;&nbsp; //1 fixed row-&gt; 1=1st data 
row<BR>&nbsp; for ptr:=firstrow to PGrid.rowcount-1 do<BR>&nbsp; 
begin<BR>&nbsp;&nbsp;&nbsp; //put sort col plus orig pointer into 
stringlist<BR>&nbsp;&nbsp;&nbsp; 
keydata1:=PGrid.cells[SortColumn1,ptr];<BR>&nbsp;&nbsp;&nbsp; 
keydata2:=PGrid.cells[SortColumn2,ptr];<BR>&nbsp;&nbsp;&nbsp; //this can be 
extended to specify decimal columns<SPAN class=495331623-01022007>/Date</SPAN> - 
ideally need more data checking though<BR>&nbsp;&nbsp;&nbsp; //if 
jkblSortGridKeyType1='I' then&nbsp; 
keydata1:=format('%12d',[strtoint(keydata1)]);<BR>&nbsp;&nbsp;&nbsp; //if 
jkblSortGridKeyType2='I' then&nbsp; 
keydata2:=format('%12d',[strtoint(keydata<SPAN 
class=495331623-01022007>2</SPAN>)]);</FONT></DIV>
<DIV><FONT face=Arial color=#0000ff size=2></FONT>&nbsp;</DIV>
<DIV><FONT face=Arial color=#0000ff size=2>&nbsp;&nbsp;&nbsp; 
tempstr:=keydata1+keydata2;<BR>&nbsp;&nbsp;&nbsp; if CaseInsensitive=true then 
uppercase(tempstr);<BR>&nbsp;&nbsp;&nbsp; xcstrpad(tempstr,60);<SPAN 
class=495331623-01022007>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//see&nbsp;below</SPAN><BR>&nbsp;&nbsp;&nbsp; 
tempstr:=tempstr+inttostr(ptr);<BR>&nbsp;&nbsp;&nbsp; 
TmpSortList.Add(tempstr);<BR>&nbsp; end;<BR>&nbsp; //sort it<BR>&nbsp; 
TmpSortList.sorted:=true;<BR>&nbsp; //now copy original cells to tempcells 
dynamic array<BR>&nbsp; //this is to keep original data as grid is now being 
overwritten<BR>&nbsp; 
setlength(tempcells,PGrid.colcount,PGrid.rowcount);<BR>&nbsp; for 
rowptr:=firstrow to PGrid.rowcount-1 do<BR>&nbsp; begin<BR>&nbsp;&nbsp;&nbsp; 
for colptr:=0 to PGrid.colcount-1 do<BR>&nbsp;&nbsp;&nbsp; 
begin<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
tempcells[colptr,rowptr]:=PGrid.cells[colptr,rowptr];<BR>&nbsp;&nbsp;&nbsp; 
end;<BR>&nbsp; end;<BR>&nbsp; //now write tempcells back to grid - note grid may 
have more than sorted list if<BR>&nbsp; //fixed rows, so 2 counters, ptr (list) 
and rowptr(grid)<BR>&nbsp; lAscDesc:=upcase(AscDesc);<BR>&nbsp; if lAscDesc='A' 
then<BR>&nbsp; begin<BR>&nbsp;&nbsp;&nbsp; for ptr:=0 to TmpSortList.Count-1 
do<BR>&nbsp;&nbsp;&nbsp; begin<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
tempstr:=copy(TmpSortList[ptr],61,10);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
oldrow:=strtoint(tempstr);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if oldrow=oldDefRow 
then NewDefRow:=ptr;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //oldptr is orig row 
number, ptr is new row number<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
rowptr:=ptr+firstrow;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; for colptr:=0 to 
PGrid.colcount-1 do<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
begin<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
PGrid.cells[colptr,rowptr]:=tempcells[colptr,oldrow];<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
end;<BR>&nbsp;&nbsp;&nbsp; end;<BR>&nbsp; end<BR>&nbsp; else<BR>&nbsp; 
begin<BR>&nbsp;&nbsp;&nbsp; for ptr:=TmpSortList.count-1 downto 0 
do<BR>&nbsp;&nbsp;&nbsp; begin<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
tempstr:=copy(TmpSortList[ptr],61,10);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
oldrow:=strtoint(tempstr);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if oldrow=oldDefRow 
then NewDefRow:=ptr;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //oldptr is orig row 
number, ptr is new row number<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
rowptr:=ptr+firstrow;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; for colptr:=0 to 
PGrid.colcount-1 do<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
begin<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
PGrid.cells[colptr,rowptr]:=tempcells[colptr,oldrow];<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
end;<BR>&nbsp;&nbsp;&nbsp; end;<BR>&nbsp; end;<BR>&nbsp; 
TmpSortList.free;<BR>&nbsp; tempcells:=nil;<BR>&nbsp; if (OldDefRow&gt;0) and 
(NewDefRow &gt; FirstRow) then PGrid.row:=NewDefRow;<BR>&nbsp; //in case row not 
set, makes sure is valid...otherwise grid can sort of scroll<BR>&nbsp; //or 
duplicate fixed row...<BR>&nbsp; if (PGrid.Row&lt;firstrow) or (PGrid.Row &gt; 
PGrid.RowCount-1) then PGrid.Row:=firstrow+1;<BR>&nbsp; 
jkblSortGridKeyType1:='';<BR>&nbsp; 
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>&nbsp; 
Rsize:=length(strec);<BR>&nbsp; if Rsize &lt; Stlen then<BR>&nbsp;&nbsp;&nbsp; 
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>&nbsp;</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>&nbsp;</DIV>
<DIV><FONT face=Arial color=#0000ff 
size=2>jkblSortGridKeyType1,jkblSortGridKeyType2:string;&nbsp; //for more sort 
types<BR></FONT></DIV>
<DIV><FONT face=Arial color=#0000ff size=2></FONT>&nbsp;</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>&nbsp;</P>
<P class=MsoAutoSig><o:p><!--StartFragment -->&nbsp;</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>