Autor Beitrag
Maxman
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 28



BeitragVerfasst: Di 13.08.02 17:35 
Hallo!

Ich möchte gerne die Zeilen in einer StringGrid sortieren (nach dem Alphabet). Ich habs mal mit folgender Procedur probiert:

ausblenden Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
procedure ordnen;
var x,l,y,n: integer;
    t: string;
begin
x:=Form2.StringGrid1.RowCount-2;
l:=0;
repeat
  Form2.Memo1.Lines[l]:=Form2.StringGrid1.Cells[0,l]+'|1|'+Form2.StringGrid1.Cells[1,l]+'|2|'+Form2.StringGrid1.Cells[2,l]+'|3|'+Form2.StringGrid1.Cells[3,l]+'|4|'+Form2.StringGrid1.Cells[4,l]+'|5|'+Form2.StringGrid1.Cells[5,l]+'|6|'+Form2.StringGrid1.Cells[6,l]+'|7|'+Form2.StringGrid1.Cells[7,l]+'|8|'+Form2.StringGrid1.Cells[8,l];
  l:=l+1;
  until l=x;
Form2.ListBox1.Items.Add(Form2.Memo1.Text);
Form2.ListBox1.Sorted:=true;
Form2.Memo1.Clear;
Form2.Memo1.Text:=Form2.ListBox1.Items.Text;
y:=Form2.Memo1.Lines.Count-2;
n:=1;
repeat
  t:=Form2.Memo1.Lines[n];
  Form2.StringGrid1.Cells[0,n]:=copy(t,1,pos(t,'|1|'));
  Form2.StringGrid1.Cells[1,n]:=copy(t,pos(t,'|1|'),pos(t,'|2|'));
  Form2.StringGrid1.Cells[2,n]:=copy(t,pos(t,'|2|'),pos(t,'|3|'));
  Form2.StringGrid1.Cells[3,n]:=copy(t,pos(t,'|3|'),pos(t,'|4|'));
  Form2.StringGrid1.Cells[4,n]:=copy(t,pos(t,'|4|'),pos(t,'|5|'));
  Form2.StringGrid1.Cells[5,n]:=copy(t,pos(t,'|5|'),pos(t,'|6|'));
  Form2.StringGrid1.Cells[6,n]:=copy(t,pos(t,'|6|'),pos(t,'|7|'));
  Form2.StringGrid1.Cells[7,n]:=copy(t,pos(t,'|7|'),pos(t,'|8|'));
  Form2.StringGrid1.Cells[7,n]:=copy(t,pos(t,'|8|'),length(t)-pos(t,'|8|'));
  n:=n+1;
  until n=y;
end;


Das klappt allerdings nicht!
Hat jemand eine Procedur parat, oder kann mir in meiner den Fehler zeigen?

Maxman
Black Lightning
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 69



BeitragVerfasst: Di 13.08.02 18:10 
ich mach grad eine Komponente Namens TSortedStringGrid, falls du willst schick ich sie dir sobald sie fertig ist.

mfg
Black Lightning

_________________
--erst programmieren, dann denken, dann debuggen--
Maxman Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 28



BeitragVerfasst: Di 13.08.02 18:57 
Hallo Black Lightning!

Das wäre echt schön von dir!

Meine E-mail Adresse ist [url]TheMaxman@gmx.de[/url].

Danke im voraus!
Maxman
Black Lightning
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 69



BeitragVerfasst: Mi 14.08.02 13:49 
im Sinne des Forums hab ich das jetzt mal online gestellt....also keine Gewähr, bin grade erst fertig geworden...ich hoffe das keine Bugs mehr drin sind.
mitglied.lycos.de/sk...ringGrid%201.002.zip

EDIT:
hab die unit jetzt noch verbessert....man kann nun auf und absteigend sortieren

_________________
--erst programmieren, dann denken, dann debuggen--
Black Lightning
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 69



BeitragVerfasst: Do 15.08.02 17:03 
konntest du es verwenden? Klappt´s?

_________________
--erst programmieren, dann denken, dann debuggen--
Maxman Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 28



BeitragVerfasst: Mi 21.08.02 12:54 
Titel: Danke!
Hallo Black Lightning!

Ich habe mir das Teil runtergeladen. Ich hab' es nur nicht geschafft es in mein Delphiprogramm einzubinden! Ich glaube, da braucht man die *.pas Datei, oder? Wenn nicht, dann wäre es nett, wenn du mir noch mal beschreiben könntest, wie ich das Ding installiere!

Danke im voraus!
Maxman
Black Lightning
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 69



BeitragVerfasst: Mi 21.08.02 16:18 
Component --> Install Component --> dann bei unit file name die sortedstringgrid.dcu datei angeben, wobei du im opendialog fenster aber den dateityp von pas zu dcu ändern musst.
dann auf compile oder install....hab es noch nicht mit einer dcu datei ausprobiert.

Dann solltest du es unter samples wieder finden.

gruß
B.L.

_________________
--erst programmieren, dann denken, dann debuggen--
Maxman Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 28



BeitragVerfasst: Mi 21.08.02 21:27 
Hallo Black Lightning!

Ich habs grad nochmal probiert, aber das Problem ist auch, dass das Compilieren nicht mal funktioniert. Die Fehlermeldung lautet:
"Mindestens eine Zeile war zu lang und musste abgeschnitten werden".
Ich kann nicht verstehen, was dabei nicht funktioniert, denn bisher klappte das Komponenteninstallieren immer!

Maxman
Black Lightning
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 69



BeitragVerfasst: Do 22.08.02 00:08 
komische Fehlermeldung.....naja nundenn hier die .pas datei: mitglied.lycos.de/sk...SortedStringGrid.pas

gruß
B.L.

_________________
--erst programmieren, dann denken, dann debuggen--
Delta7
Hält's aus hier
Beiträge: 12



BeitragVerfasst: Fr 23.08.02 02:57 
Hi Leute,

Ich hab mal auf die schnelle was gebastelt......
(man verzeihe mir den button1 und das StringGrid1

ausblenden volle Höhe Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, Grids;

type
  TForm1 = class(TForm)
    Button1: TButton;
    StringGrid1: TStringGrid;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation
    var SortColumn:integer;
{$R *.DFM}

function SortCompare(p1,p2:pointer):integer;
  begin
     //Pointer auf tStringList casten und die Spalte als index angeben - schon haben wird die Daten wieder
    result:=StrComp(pChar(tStringlist(p1)[SortColumn]),pChar(tStringlist(p2)[SortColumn])); 
  end;

procedure SortStringGridByColumn(StrGrid:tStringGrid;SortCol:integer);
  var List:tList;
      i:integer;
      StringList:tStringList;
begin
  List:=tList.Create;
  for i:=0 to StrGrid.RowCount-1 do
    begin
      StringList:=tStringList.Create;
      StringList.AddStrings(StrGrid.Rows[i]);
      List.Add(StringList);
    end;
  if SortCol<StrGrid.RowCount then SortColumn:=SortCol else SortColumn:=StrGrid.RowCount; //Globale Variable für die zu sortierende Spalte Setzen
  List.Sort(SortCompare);
  for i:=0 to List.Count-1 do
    begin
      StrGrid.Rows[i]:=tStringList(List.items[i]);
      tStringList(List.items[i]).Free;
    end;
  List.Free;

end;

procedure TForm1.Button1Click(Sender: TObject);
begin
 SortStringGridByColumn(StringGrid1,1);
end;

end.


Das Ganze kann man natürlich noch nett in eine eigene zUnit verpacken, aber das überlasse ich Euch.

@ProfiCoders: Kann mal einer schnell drüberschauen, ob ich auch den Speicher brav aufgeräumt habe ? *g* THX

Gruss
Thomas

_________________
Ein Byte hat 8 Bit. Wer's nicht glaubt, soll seinen Rechner aufschrauben und nachzählen.