was ist neu ¦  programmier tips ¦  indy artikel ¦  intraweb artikel ¦  informationen ¦  links ¦  interviews
 sonstiges ¦  tutorials ¦  Add&Win Gewinnspiel

Tips (1541)

Dateien (137)
Datenbanken (90)
Drucken (35)
Grafik (114)
IDE (21)
Indy (5)
Internet / LAN (130)
IntraWeb (0)
Mathematik (76)
Multimedia (45)
Oberfläche (107)
Objekte/
ActiveX (51)

OpenTools API (3)
Sonstiges (126)
Strings (83)
System (266)
VCL (242)

Tips sortiert nach
Komponente


Tip suchen

Tip hinzufügen

Add&Win Gewinnspiel

Werbung

38 Visitors Online


 
...ein StringGrid nach Spalten sortieren?
Autor: Chris Willig
Homepage: http://www.5thelephant.com
[ Tip ausdrucken ]  

Tip Bewertung (27):  
     


type
  
TMoveSG = class(TCustomGrid); // reveals protected MoveRow procedure

{...}

procedure SortGridByCols(Grid: TStringGrid; ColOrder: array of Integer);
var
  
i, j:   Integer;
  Sorted: Boolean;

function Sort(Row1, Row2: Integer): Integer;
var
  
C: Integer;
begin
  
C      := 0;
  Result := AnsiCompareStr(Grid.Cols[ColOrder[C]][Row1], Grid.Cols[ColOrder[C]][Row2]);
  if Result = 0 then
  begin
    
Inc(C);
    while (C <= High(ColOrder)) and (Result = 0) do
    begin
      
Result := AnsiCompareStr(Grid.Cols[ColOrder[C]][Row1],
        Grid.Cols[ColOrder[C]][Row2]);
      Inc(C);
    end;
  end;
end;

begin
  if 
SizeOf(ColOrder) div SizeOf(i) <> Grid.ColCount then Exit;

  for i := 0 to High(ColOrder) do
    if 
(ColOrder[i] < 0) or (ColOrder[i] >= Grid.ColCount) then Exit;

  j := 0;
  Sorted := False;
  repeat
    
Inc(j);
    with Grid do
      for 
i := 0 to RowCount - 2 do
        if 
Sort(i, i + 1) > 0 then
        begin
          
TMoveSG(Grid).MoveRow(i + 1, i);
          Sorted := False;
        end;
  until Sorted or (j = 1000);
  Grid.Repaint;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  
{ Sort rows based on the contents of two or more columns.
    Sorts first by column 1. If there are duplicate values
    in column 1, the next sort column is column 2 and so on...}
  
SortGridByCols(StringGrid1, [1, 2, 0, 3, 4]);
end;

 

Bewerten Sie diesen Tipp:

dürftig
ausgezeichnet


Copyright © by SwissDelphiCenter.ch
All trademarks are the sole property of their respective owners