0

I have a string grid, from which i can delete columns. I defined a CustomStringGrid type that allows me to use DeleteColumn method.

This is how it looks:

TCustomStringGrid = class(TStringGrid)

[...]
With tCustomStringGrid(mygrid) do
DeleteColumn(col)
end;

IS there something similar to add a column? I've tried InsertColumn but it doesn't seem to exist. I want to add a column at a particular position. In fact, if a user deletes a column i have an undo button which i want to reinsert the deleted column (i'm keeping the data in an array so i can recreate the column but i don't know how to insert one in a particular position).

Thank you!

user28470
  • 419
  • 5
  • 17
  • No. You will have to set the `ColCount` and shift all the columns by yourself (`TStringGridStrings.Insert` method has a clear exception message for that *"Cannot insert or delete rows from grid"*). Welcome to a string grid hell :-) – TLama Sep 23 '14 at 10:41

2 Answers2

1

It's not built in but easy to emulate, with ColCount = ColCount + 1 and MoveColumn from a HackClass.

type
  THackGrid=Class(Grids.TCustomGrid)
  End;

Procedure InsertColumn(G:TStringGrid;Position:Integer);
begin
  if Position<G.ColCount then
    begin
      G.ColCount := G.ColCount + 1;
      THackGrid(g).MoveColumn(G.ColCount - 1,Position);
    end;
end;

procedure TMyForm.Button1Click(Sender: TObject);
begin
    InsertColumn(StringGrid1,1);
end;
bummi
  • 27,123
  • 14
  • 62
  • 101
0

THack grid is not working, maybe it is ok when both cols are visible, but that works always :

Procedure MoveColumn(G:TStringGrid;OldPosition : integer;NewPosition:Integer);
var
  i : integer;
  temp : string;
  begin
  for i := 0 to g.rowcount - 1 do
    begin
    temp := g.cells[OldPosition,i];
    g.cells[OldPosition,i] := g.cells[NewPosition,i];
    g.cells[NewPosition,i] := temp;
    end;
  end;
bummi
  • 27,123
  • 14
  • 62
  • 101