StringGrid - adicionando, removendo e ordenando uma coluna

Top  Previous  Next

procedure GridRemoveColumn(StrGrid: TStringGrid; DelColumn: Integer); 

var 

  Column: Integer; 

begin 

  If DelColumn <= StrGrid.ColCount then 

  begin 

    For Column := DelColumn To StrGrid.ColCount-2 do 

      StrGrid.Cols[Column].Assign(StrGrid.Cols[Column+1]); 

    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 SortGrid(Grid: TStringGrid; SortCol: Integer);

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

        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;