Announcement

Collapse
No announcement yet.

StringGrid Sortieren

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • StringGrid Sortieren

    Hallo.

    Ich möchte den Inhalt eines Stringgrids alphabetisch sortieren, bekomme den Code dafür aber nicht gebacken.

    Kann mit jemand helfen?

    P.S.: Folgender Code (abgeleitet von <a href="http://www.dsdt.info/tipps/?id=378" Target="_Blank">SelectionSort</a>) <u>funktioniert nicht</u>:

    <b>procedure</b> TForm1.Sort(Down: Boolean);
    <b>var</b> i, j, Pos: Integer;
    <b>begin</b>
    &nbsp;&nbsp;<b>for</b> i := 0 <b>to</b> StringGrid1.RowCount - 2 <b>do</b>
    &nbsp;&nbsp;&nbsp;&nbsp;<b>begin</b>
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Pos := i;
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<b>for</b> j := i + 1 <b>to</b> StringGrid1.RowCount - 1 <b>do</b>
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<b >if</b> Down <b>then</b>
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&n bsp;&nbsp;<b>if </b>StringGrid1.Cells[j,0] > StringGrid1.Cells[Pos,0] <b>then</b>
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&n bsp;&nbsp;&nbsp;&nbsp;Pos := j
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<b >else</b>
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&n bsp;&nbsp;<b>if </b>StringGrid1.Cells[j,0] < StringGrid1.Cells[Pos,0] <b>then</b>
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&n bsp;&nbsp;&nbsp;&nbsp;Pos := j;
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;StringGrid1.Ro ws[j].Exchange(Pos,i)
    &nbsp;&nbsp;&nbsp;&nbsp;<b>end;</b>
    <b>end;</b>
    If it's there and you can see it – it's REAL
    If it's there and you can't see it – it's TRANSPARENT
    If it's not there and you can see it – it's VIRTUAL
    If it's not there and you can't see it – it's GONE!

  • #2
    Habs schon selbst rausgekriegt:

    procedure TFormSort.SortStringGrid(GenStrGrid: TStringGrid; ThatCol: Integer);
    const
    // Define the Separator
    TheSeparator = '@';
    var
    CountItem, I, J, K, L, ThePosition: integer;
    MyList: TStringList;
    MyString, TempString: string;
    begin
    CountItem := GenStrGrid.RowCount;
    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);
    Mylist.Sort;

    for K := 1 to Mylist.Count do
    begin
    MyString := MyList.Strings[(K - 1)];
    ThePosition := Pos(TheSeparator, MyString);
    TempString := '';
    TempString := Copy(MyString, (ThePosition + 1), Length(MyString));
    MyList.Strings[(K - 1)] := '';
    MyList.Strings[(K - 1)] := TempString;
    end;

    for L := 0 to MyList.Count do
    begin

    end;

    for J := 1 to (CountItem - 1) do
    GenStrGrid.Rows[J].Text := MyList.Strings[(J - 1)];
    end;
    finally
    MyList.Free;
    end;
    end
    If it's there and you can see it – it's REAL
    If it's there and you can't see it – it's TRANSPARENT
    If it's not there and you can see it – it's VIRTUAL
    If it's not there and you can't see it – it's GONE!

    Comment

    Working...
    X