0

I am new to Delphi creating runtime Tgrid and want to add some rows and columns. I googled but not getting any thing. i am at level zero. what i have tried i mentioned it below.This code is showing nothing on my form.

procedure TForm1.Button1Click(Sender: TObject);
var
  Grid : TGrid;
begin
  Grid := TGrid.Create(Form1);
  Grid.Visible := True;
  Grid.Margins.Left := 10;
  Grid.Margins.Right := 10;
  Grid.Margins.Top := 10;
  Grid.Margins.Bottom := 10;
  Grid.RowCount := 5;
end;
Tom Brunberg
  • 20,312
  • 8
  • 37
  • 54
  • 1
    This Q is essentially a sub-Q of https://stackoverflow.com/questions/1005373/creating-components-at-runtime-delphi. – Andreas Rejbrand May 25 '20 at 16:39
  • What's stopping you simply placing the Grid on the form at design-time with its Visible property set to False and at run-time simply set its Visible to true when you want it to be? – MartynA May 25 '20 at 19:00
  • Please do not edit your question once you have already received answers. Specificly do not destroy edits that others have done, unless you are very sure, that you need to do so. I rolled back to the previous version. – Tom Brunberg May 26 '20 at 14:03

1 Answers1

1

The grid control must have a parent control. Indeed, naturally, the system must know where (on the screen) to put your control!

You probably want the grid to have the form as its parent. If so, just add Grid.Parent := Form1; after the construction of the grid.

Of course, when you add this missing line of code and get to see the grid, you'll very soon notice that your Margins assignments have no effect. That's because by default you control the position of the control (no pun intended) manually using its Top, Left, Height, and Width properties.

But should you start experimenting with the Align property, you'll discover the effect of the margins, if you also set AlignWithMargins to True. For instance, if you set Align to alClient, the control will occupy all of its parent's client area, save the margins.

Andreas Rejbrand
  • 105,602
  • 8
  • 282
  • 384
  • 1
    + There is no benefits from setting `TMargins` without using `TAlign` – Ilyes May 25 '20 at 16:41
  • 2
    @Sami is quite right. The margins will only have any effect if you also set the `Align` property to something other than `alNone`, and also set `AlignWithMargins` to `True`. – Andreas Rejbrand May 25 '20 at 16:45
  • You can always edit the answer to include more helpful stuff @Andreas Rejbrand :) – Ilyes May 25 '20 at 16:47
  • Thanks all for your reply it really helped me. procedure TForm1.Button1Click(Sender: TObject); var Grid : TGrid; begin Grid := TGrid.Create(Form1); Grid.Visible := True; Grid.Parent := ListBox1; Grid.Align := Grid.Align.alClient; end; now its working but how to add columns ? if you find any proper example online please let me know that shows proper workout on it. – Dharmesh Bhagatwala May 25 '20 at 17:04
  • 1
    Having a listbox control as parent is unusual, especially when you use `alClient` (the RHS can be `alClient` alone), because then the entire list box will be hidden! – Andreas Rejbrand May 25 '20 at 17:09
  • 2
    @DharmeshBhagatwala To create a column, write `TColumn.Create(Grid).Parent := Grid;`. The column is created with `Grid` as owner and parent. Access a column´s properties with `Grid.Columns[i]` where `i` is the 0-based index. – Tom Brunberg May 25 '20 at 20:46