Примеры добавления, удаления и
сортировки столбцов в StringGrid.
Компилятор: Delphi
Вариант 1:
Удаление колонки:
Procedure GridRemoveColumn(StrGrid: TStringGrid; DelColumn: Integer);
Var Column: Integer;
begin
If DelColumn <= StrGrid.ColCount then
Begin
For Column := DelColumn To StrGrid.ColCount-1 do
StrGrid.Cols[Column-1].Assign(StrGrid.Cols[Column]);
StrGrid.ColCount := StrGrid.ColCount-1;
End;
end;
Добавление колонки:
Procedure GridAddColumn(StrGrid: TStringGrid; NewColumn: Integer);
Var Column: Integer;
begin
StrGrid.ColCount := StrGrid.ColCount+1;
For Column := StrGrid.ColCount-1 downto NewColumn do
StrGrid.Cols[Column].Assign(StrGrid.Cols[Column-1]);
StrGrid.Cols[NewColumn-1].Text := '';
end;
Сортировка столбца:
Procedure GridSort(StrGrid: TStringGrid; NoColumn: Integer);
Var Line, PosActual: Integer;
Row: TStrings;
begin
Renglon := TStringList.Create;
For Line := 1 to StrGrid.RowCount-1 do
Begin
PosActual := Line;
Row.Assign(TStringlist(StrGrid.Rows[PosActual]));
While True do
Begin
If (PosActual = 0) Or
(StrToInt(Row.Strings[NoColumn-1]) >=
StrToInt(StrGrid.Cells[NoColumn-1,PosActual-1]))
then
Break;
StrGrid.Rows[PosActual] := StrGrid.Rows[PosActual-1];
Dec(PosActual);
End;
If StrToInt(Row.Strings[NoColumn-1]) <
StrToInt(StrGrid.Cells[NoColumn-1,PosActual]) then
StrGrid.Rows[PosActual] := Row;
End;
Renglon.Free;
end;
Вариант 2:
Удаление колонки:
procedure RemoveColumn(SG : TStringGrid; ColNumber : integer);
var Column : integer;
begin
ColNumber := abs(ColNumber);
if ColNumber <= SG.ColCount then begin
for Column := ColNumber to SG.ColCount - 2 do begin
SG.Cols[Column].Assign(SG.Cols[Column +
1]);
SG.Colwidths[Column] :=
SG.Colwidths[Column + 1];
end;
SG.ColCount := SG.ColCount - 1;
end;
end;
Добавление колонки:
procedure AddColumn(SG : TStringGrid; AtColNumber : integer;
ColWidth
: integer = 0);
var Column : integer;
Wdth : integer;
begin
AtColNumber := abs(AtColNumber);
SG.ColCount := SG.ColCount + 1;
if abs(ColWidth) = 0 then
Wdth := SG.DefaultColWidth
else
Wdth := ColWidth;
if AtColNumber <= SG.ColCount then begin
for Column := SG.ColCount - 1 downto AtColNumber + 1 do begin
SG.Cols[Column].Assign(SG.Cols[Column - 1]);
SG.Colwidths[Column] := SG.Colwidths[Column - 1];
end;
SG.Cols[AtColNumber].Text := '';
SG.Colwidths[AtColNumber] := Wdth;
end;
end;
Сортировка столбца:
type TStringGridExSortType = (srtAlpha,srtInteger,srtDouble);
procedure GridSort(SG : TStringGrid; ByColNumber,FromRow,ToRow : integer;
SortType
: TStringGridExSortType = srtAlpha);
var Temp : TStringList;
function SortStr(Line : string) : string;
var RetVar : string;
begin
case SortType of
srtAlpha : Retvar
:= Line;
srtInteger : Retvar :=
FormatFloat('000000000',StrToIntDef(trim(Line),0));
srtDouble : try
Retvar
:= FormatFloat('000000000.000000',StrToFloat(trim(Line)));
except
RetVar :=
'0.00';
end;
end;
Result := RetVar;
end;
// Рекурсивный QuickSort
procedure QuickSort(Lo,Hi : integer; CC : TStrings);
procedure Sort(l,r: integer);
var i,j : integer;
x :
string;
begin
i := l; j := r;
x := SortStr(CC[(l+r) DIV 2]);
repeat
while
SortStr(CC[i]) < x do inc(i);
while x <
SortStr(CC[j]) do dec(j);
if i <= j then
begin
Temp.Assign(SG.Rows[j]); //
Меняем местами 2 строки
SG.Rows[j].Assign(SG.Rows[i]);
SG.Rows[i].Assign(Temp);
inc(i);
dec(j);
end;
until i > j;
if l < j then sort(l,j);
if i < r then sort(i,r);
end;
begin {quicksort};
Sort(Lo,Hi);
end;
begin
Temp := TStringList.Create;
QuickSort(FromRow,ToRow,SG.Cols[ByColNumber]);
Temp.Free;
end;
|