简体   繁体   中英

Insert column into string grid, delphi

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!

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;

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM