Einzelnen Beitrag anzeigen

Klaus01

Registriert seit: 30. Nov 2005
Ort: München
5.755 Beiträge
 
Delphi 10.4 Sydney
 
#8

Re: Klick auf Kopfzeile von StringGrid -> sortieren lasse

  Alt 8. Jan 2010, 18:19
Hallo,

ja da habe ich es falsch verstanden.

Mit folgendem Code habe ich in einem Projekt das StringGrid sortieren lassen.
Die Methode hat auch funktoniert wenn das StringGird horizontal gescrollt war.
Die SpaltenNummern der Spalteköpfe ändert sich ja durch das scrollen nicht.

Delphi-Quellcode:
procedure TForm1.SortStringGrid(GenStrGrid: TStringGrid; ThatCol: Array of 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[0]] + 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;
[Quelle: unbekannt]

Grüße
Klaus
Klaus
  Mit Zitat antworten Zitat