<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META http-equiv=Content-Type content="text/html; charset=iso-8859-1">
<META content="MSHTML 6.00.6001.18148" name=GENERATOR>
<STYLE></STYLE>
</HEAD>
<BODY bgColor=#ffffff>
<DIV><FONT face=Arial color=#000000 size=2>If its a DBGrid then you need to
either</FONT></DIV>
<DIV><FONT face=Arial color=#000000 size=2></FONT> </DIV>
<DIV><FONT face=Arial color=#000000 size=2>-Change the order of the dataset
records - which means defining an index,</FONT></DIV>
<DIV><FONT face=Arial color=#000000 size=2>-Or use a grid like the CSDBGrid
(freeware been around forever) that allows you to click on the column
headers to sort, like Windows Explorer.</FONT></DIV>
<DIV><FONT face=Arial color=#000000 size=2></FONT> </DIV>
<DIV><FONT face=Arial color=#000000 size=2>If its a stringgrid then You can sort
a stringgrid nicely as well by sorting the data:</FONT></DIV>
<DIV><FONT face=Arial color=#000000 size=2></FONT> </DIV>
<DIV><FONT face=Arial color=#000000 size=2>here is an example:</FONT></DIV>
<DIV><FONT face=Arial color=#000000 size=2></FONT> </DIV>
<DIV><FONT face=Arial color=#000000 size=2>procedure xcSortGrid(var PGrid:
TStringGrid; SortColumn1: Integer; SortColumn2: integer; AscDesc: char;
CaseInSensitive: Boolean);<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></FONT><FONT face=Arial color=#000000
size=2></FONT></DIV>
<DIV><FONT face=Arial color=#000000 size=2> tempstr :=
keydata1 + keydata2;<BR> if CaseInsensitive = true then
uppercase(tempstr);<BR> xcstrpad(tempstr,
60); //this pads string
to 60 chars long<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>end;<BR></FONT></DIV>
<DIV><FONT face=Arial color=#000000 size=2></FONT> </DIV>
<DIV>John</DIV>
<DIV> </DIV></BODY></HTML>