[DUG] Database sequence sort

John Bird johnkbird at paradise.net.nz
Sat Dec 6 15:10:38 NZDT 2008


If its a DBGrid then you need to either

-Change the order of the dataset records  - which means defining an index,
-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.

If its a stringgrid then You can sort a stringgrid nicely as well by sorting the data:

here is an example:

procedure xcSortGrid(var PGrid: TStringGrid; SortColumn1: Integer; SortColumn2: integer; AscDesc: char; CaseInSensitive: Boolean);
var
  TmpSortList: TStringList;
  olddefrow, newdefrow: integer; //keep same data row as default
  oldrow, datalhs, ptr, colptr, rowptr: integer;
  keydata1, keydata2, tempstr: string;
  firstrow: integer; //first row after fixed rows
  lAscDesc: char;
  tempcells: array of array of string;
begin
  if PGrid.rowcount < 2 then exit; //nothing to sort
  TmpSortList := TStringList.Create;
  TmpSortList.sorted := false;
  olddefrow := -1;
  NewDefRow := -1;
  if PGrid.row > 0 then oldDefRow := PGrid.row;
  firstrow := 0;
  if PGrid.FixedRows > 0 then
    firstrow := PGrid.FixedRows; //1 fixed row-> 1=1st data row
  for ptr := firstrow to PGrid.rowcount - 1 do
  begin
    //put sort col plus orig pointer into stringlist
    keydata1 := PGrid.cells[SortColumn1, ptr];
    keydata2 := PGrid.cells[SortColumn2, ptr];

    tempstr := keydata1 + keydata2;
    if CaseInsensitive = true then uppercase(tempstr);
    xcstrpad(tempstr, 60);            //this pads string to 60 chars long
    tempstr := tempstr + inttostr(ptr);
    TmpSortList.Add(tempstr);
  end;
  //sort it
  TmpSortList.sorted := true;
  //now copy original cells to tempcells dynamic array
  //this is to keep original data as grid is now being overwritten
  setlength(tempcells, PGrid.colcount, PGrid.rowcount);
  for rowptr := firstrow to PGrid.rowcount - 1 do
  begin
    for colptr := 0 to PGrid.colcount - 1 do
    begin
      tempcells[colptr, rowptr] := PGrid.cells[colptr, rowptr];
    end;
  end;
  //now write tempcells back to grid - note grid may have more than sorted list if
  //fixed rows, so 2 counters, ptr (list) and rowptr(grid)
  lAscDesc := upcase(AscDesc);
  if lAscDesc = 'A' then
  begin
    for ptr := 0 to TmpSortList.Count - 1 do
    begin
      tempstr := copy(TmpSortList[ptr], 61, 10);
      oldrow := strtoint(tempstr);
      if oldrow = oldDefRow then NewDefRow := ptr;
      //oldptr is orig row number, ptr is new row number
      rowptr := ptr + firstrow;
      for colptr := 0 to PGrid.colcount - 1 do
      begin
        PGrid.cells[colptr, rowptr] := tempcells[colptr, oldrow];
      end;
    end;
  end
  else
  begin
    for ptr := TmpSortList.count - 1 downto 0 do
    begin
      tempstr := copy(TmpSortList[ptr], 61, 10);
      oldrow := strtoint(tempstr);
      if oldrow = oldDefRow then NewDefRow := ptr;
      //oldptr is orig row number, ptr is new row number
      rowptr := ptr + firstrow;
      for colptr := 0 to PGrid.colcount - 1 do
      begin
        PGrid.cells[colptr, rowptr] := tempcells[colptr, oldrow];
      end;
    end;
  end;
  TmpSortList.free;
  tempcells := nil;
  if (OldDefRow > 0) and (NewDefRow > FirstRow) then PGrid.row := NewDefRow;
  //in case row not set, makes sure is valid...otherwise grid can sort of scroll
  //or duplicate fixed row...
  if (PGrid.Row < firstrow) or (PGrid.Row > PGrid.RowCount - 1) then PGrid.Row := firstrow + 1;
end;


John
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://listserver.123.net.nz/pipermail/delphi/attachments/20081206/80b4bb57/attachment.html 


More information about the Delphi mailing list