Topic: Table Grid columns width

Hello MVDers,

Just quick question... I can't find option for resizing table grid columns width when resizing window. I set up anchors for table grid to ALL but I would like to auto resize collumns width when resizing window.

Thanks in advance

Re: Table Grid columns width

Hello.


You can do it using this simple script

procedure Form1_TableGrid1_OnResize (Sender: TObject);
var
    i, c: integer;
begin
    c := Form1.TableGrid1.Columns.Count-1;
    for i := 0 to c do
    begin
        Form1.TableGrid1.Columns[i].Width := Form1.TableGrid1.ClientWidth div Form1.TableGrid1.Columns.Count;
    end;
end;
Dmitry.

3 (edited by amadeus.sync 2020-03-13 15:42:50)

Re: Table Grid columns width

Thanks man, works perfectly.

Edit: Is it possible to keep columns width proportions? With this script, all columns width are same size.

Thanks again Dmitry.

Re: Table Grid columns width

DriveSoft wrote:

Hello.


You can do it using this simple script

procedure Form1_TableGrid1_OnResize (Sender: TObject);
var
    i, c: integer;
begin
    c := Form1.TableGrid1.Columns.Count-1;
    for i := 0 to c do
    begin
        Form1.TableGrid1.Columns[i].Width := Form1.TableGrid1.ClientWidth div Form1.TableGrid1.Columns.Count;
    end;
end;

Edit 2: corrected one line of code:

frmPreglediPretraga.TableGrid1.Columns[i].Width := frmPreglediPretraga.TableGrid1.ClientWidth div c ;

Re: Table Grid columns width

Example for three columns: 50+30+20=100%

procedure Form1_TableGrid1_OnResize (Sender: TObject);
begin
    Form1.TableGrid1.Columns[0].Width := Trunc(Form1.TableGrid1.ClientWidth * (50 / 100)); // 50%
    Form1.TableGrid1.Columns[1].Width := Trunc(Form1.TableGrid1.ClientWidth * (30 / 100)); // 30%
    Form1.TableGrid1.Columns[2].Width := Trunc(Form1.TableGrid1.ClientWidth * (20 / 100)); // 20%
end;
Dmitry.

Re: Table Grid columns width

DriveSoft wrote:

Example for three columns: 50+30+20=100%

procedure Form1_TableGrid1_OnResize (Sender: TObject);
begin
    Form1.TableGrid1.Columns[0].Width := Trunc(Form1.TableGrid1.ClientWidth * (50 / 100)); // 50%
    Form1.TableGrid1.Columns[1].Width := Trunc(Form1.TableGrid1.ClientWidth * (30 / 100)); // 30%
    Form1.TableGrid1.Columns[2].Width := Trunc(Form1.TableGrid1.ClientWidth * (20 / 100)); // 20%
end;


Perfect !
Thanks again.

Re: Table Grid columns width

DriveSoft wrote:

Example for three columns: 50+30+20=100%

procedure Form1_TableGrid1_OnResize (Sender: TObject);
begin
    Form1.TableGrid1.Columns[0].Width := Trunc(Form1.TableGrid1.ClientWidth * (50 / 100)); // 50%
    Form1.TableGrid1.Columns[1].Width := Trunc(Form1.TableGrid1.ClientWidth * (30 / 100)); // 30%
    Form1.TableGrid1.Columns[2].Width := Trunc(Form1.TableGrid1.ClientWidth * (20 / 100)); // 20%
end;

For some reason, after implementing this script (which works fine) I have another problem. On my main form (frmHome)  I have button1 to show Form1 with TableGrid1 (form and grid in script). When I click button1, frmHome minimizes and it's impossiblle to restore it. I tried to solve problem adding script:

procedureForm1_OnClose (Sender: String; Action: string);
begin
    frmHome.Show;
end;

but after closing Form1, frmHome is still minimized and can't be restored.

Sorry for so many questions, I'm still learning and thanks for helping me.

8 (edited by derek 2020-03-13 18:47:51)

Re: Table Grid columns width

Hi,
Can you upload your project (zipped and without the executable) and someone can take a look for you?
Derek.

Re: Table Grid columns width

derek wrote:

Hi,
Can you upload your project (zipped and without the executable) and someone can take a look for you?
Derek.


There it is.
Thanks for helping.

Post's attachments

Attachment icon amadeus.rar 306.09 kb, 255 downloads since 2020-03-13 

Re: Table Grid columns width

You should use event OnResize of the TableGrid component, not form.


instead

procedure frmReview_OnResize (Sender: TObject; Action: string);

you should use

procedure frmReview_TableGrid1_OnResize (Sender: TObject);
Dmitry.

Re: Table Grid columns width

DriveSoft wrote:

You should use event OnResize of the TableGrid component, not form.


instead

procedure frmReview_OnResize (Sender: TObject; Action: string);

you should use

procedure frmReview_TableGrid1_OnResize (Sender: TObject);

Yea, that works. Thanks.