Einzelnen Beitrag anzeigen

dopeline

Registriert seit: 7. Mär 2003
Ort: Berlin
304 Beiträge
 
Delphi 7 Enterprise
 
#3
  Alt 13. Mai 2003, 07:42
Delphi-Quellcode:
procedure SortStringGrid(var GenStrGrid: TStringGrid; ThatCol: Integer);
const
  // Define the Separator
  TheSeparator = '@';
var
  CountItem, I, J, K, ThePosition: integer;
  MyList: TStringList;
  MyString, TempString: string;
begin
  // Give the number of rows in the StringGrid
  CountItem := GenStrGrid.RowCount;
  //Create the List
  MyList := TStringList.Create;
  MyList.Sorted := False;
  try
    begin
      for I := 1 to (CountItem - 1) do
        MyList.Add(GenStrGrid.Rows[I].Strings[ThatCol] + TheSeparator +
          GenStrGrid.Rows[I].Text);
      //Sort the List
      Mylist.Sort;

      for K := 1 to Mylist.Count do
      begin
        //Take the String of the line (K – 1)
        MyString := MyList.Strings[(K - 1)];
        //Find the position of the Separator in the String
        ThePosition := Pos(TheSeparator, MyString);
        TempString := '';
        {Eliminate the Text of the column on which we have sorted the StringGrid}
        TempString := Copy(MyString, (ThePosition + 1), Length(MyString));
        MyList.Strings[(K - 1)] := '';
        MyList.Strings[(K - 1)] := TempString;
      end;

      // Refill the StringGrid
      for J := 1 to (CountItem - 1) do
        GenStrGrid.Rows[J].Text := MyList.Strings[(J - 1)];
    end;
  finally
    //Free the List
    MyList.Free;
  end;
end;
und zum aufrufen der prozedur benutzt du:
Delphi-Quellcode:
procedure TFormHaupt.Vorname1Click(Sender: TObject);
begin
SortStringGrid(StringGrid1, 0); // 0 steht für die Spalte, die sortiert werden soll
end;
das funktioniert (getestet)

Grüße, dopeline
  Mit Zitat antworten Zitat