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!

1
  • 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 :-) Commented Sep 23, 2014 at 10:41

2 Answers 2

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;
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! Easy solution :D
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;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.