StringGrid2.Rows[j] :=StringGrid2.Rows[j+1];
procedure SortStringGrid( var GenStrGrid: TStringGrid; ThatCol: integer); const TheSeparator = '@'; var CountItem, I, J, K, 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 := ''; {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; for J := 1 to (CountItem - 1) do GenStrGrid.Rows[J].Text := MyList.Strings[(J - 1)]; end; finally MyList.Free; end; end; procedure TForm1.Button1Click(Sender: TObject); begin //trier le Stringgrid d'après la première colonne SortStringGrid(StringGrid1, 1); end;
unit U_Gridsort; {Sample program to sort rows of a stringgrid based on any selected column} interface uses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, Grids, StdCtrls; type TForm1 = class(TForm) StringGrid1: TStringGrid; Label1: TLabel; procedure FormCreate(Sender: TObject); procedure StringGrid1MouseUp(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer); private { Private declarations } public { Public declarations } procedure SortGrid(Grid : TStringGrid; SortCol:integer); end; var Form1: TForm1; implementation {$R *.DFM} {************** SortGrid *************} procedure Tform1.Sortgrid(Grid : TStringGrid; SortCol:integer); {A simple exchange sort of grid rows} var i,j : integer; temp:tstringlist; begin temp:=tstringlist.create; with Grid do for i := FixedRows to RowCount - 2 do {because last row has no next row} for j:= i+1 to rowcount-1 do {from next row to end} if AnsiCompareText(Cells[SortCol, i], Cells[SortCol,j]) > 0 then begin temp.assign(rows[j]); rows[j].assign(rows[i]); rows[i].assign(temp); end; temp.free; end; procedure TForm1.FormCreate(Sender: TObject); var i,j:integer; totwidth:integer; begin with stringgrid1 do begin {assign some random numbers} for j := 1 to rowcount-1 do for i:= 1 to colcount-1 do cells[i,j]:=format('%3d',[random(1000)]); {Add row labels} for i:= 1 to rowcount-1 do cells[0,i]:='Row '+ format('%2d',[i]); {Add column labels and change column widths} totwidth:=0; for i:= 0 to colcount-1 do begin cells[i,0]:='Column '+ inttostr(i+1); {vary column widths for testing} colwidths[i]:=defaultcolwidth+random(48); inc(totwidth,colwidths[i]); end; {adjust grid width so that all column are visible} width:=totwidth+(colcount+3)*gridlinewidth; end; end; {************ StringgridMouseUp **************} procedure TForm1.StringGrid1MouseUp(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer); var c,j:integer; rect:trect; begin with stringgrid1 do if y<rowheights[0] then {make sure row 0 was clicked} begin for j:= 0 to colcount-1 do {determine which column was clicked} begin rect := cellrect(j,0); if (rect.Left < x) and (rect.Right> x) then begin c := j; break; end; end; sortgrid(stringgrid1,c); end; end; end.
Les membres obtiennent plus de réponses que les utilisateurs anonymes.
Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.
Le fait d'être membre vous permet d'avoir des options supplémentaires.