RE:Como ordenar una StringGrid
Publicado por
Sagitarius (1 intervención) el 07/02/2007 02:21:23
{ ---------------------------------------------------------------------------- }
procedure TForm1.Sortgrid(Grid : TStringGrid; SortCol:integer; by: byte);
var
i,j : Integer;
temp : TStringList;
begin
Temp:= TStringList.Create;
with Grid
do begin
for i := FixedRows to RowCount - 2 do
begin
for j:= i+1 to rowcount-1 do
begin
if (by=0) then // ascendente
begin
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;
end
else // descendente
begin
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;
end;
end;
end;
end;
temp.free;
change:=not by;
end;
{ ---------------------------------------------------------------------------- }
procedure TForm1.StringGrid1MouseDown(Sender: TObject;
Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
var ACol,ARow: integer;
begin
StringGrid1.MouseToCell(X, Y, ACol, ARow);
// solo debes seleccionar la columna correspondiente.
if (ACol>0) and (ARow=0) then
Form1.Sortgrid(StringGrid1,Acol,change);
end;
{ ---------------------------------------------------------------------------- }
nota:
declarar la variable change en la seccion private o publica
Sacado del Club Delphi.