Jump to content


Photo

Applying MinWidth to AutoSize columns


  • Please log in to reply
2 replies to this topic

#1 mauricei

mauricei
  • Members
  • 4 posts

Posted 24 March 2023 - 09:16 PM

Hi,

 

To be able to fill the width of the grid I would like to set the last column as AutoSize so it fills all remaining space.

 

I would still like to be able to apply a minimum width to the column so it does not disappear completely if the grid is resized to the point where that column no longer fits in the visible space. This way I could get a horizontal scrollbar and still scroll to it.

 

There also seems to be a bug. Once the AutoSize column shrinks to a width of 0 the value for Columns.FlexibleWidth is 0. This means that in the TNxListGridView6.ResizeBy method there are no further calls to Columns.UpdateAutoSizes and the column with AutoSize nevers reappears even when the grid is stretched to a width that would make the column visible.

 

Is there a way I can apply a minimum width to an AutoSize column?

 

Thank you



#2 mauricei

mauricei
  • Members
  • 4 posts

Posted 25 March 2023 - 12:44 AM

If it can help someone, here is the code for the workaround I'm using. It's seems to be working for my needs but might not account for every eventuality.

 

Basically, on resize and column move, if the last column is smaller than it's fixed width I remove the AutoSize. If there is blank space after the last column then I turn AutoSize back on.

 

// This is set as a callback for OnColumnMoveDrop;
procedure TMyGrid.ColumnMoveDrop (Sender: TObject; ACol, OldPosition, NewPosition: Integer);
begin
    if OldPosition = NewPosition then
        Exit;

    { If column was previouly the last one }
    if OldPosition = ActiveView.Columns.Count - 1 then
        ActiveView.Columns[ACol].AutoSize := False;

    AdjustLastColumnAutoSize;
end;

{-----------------------------------------------------------------------------}

procedure TMyGrid.WMSize(var Message: TWMSize);
begin
    inherited WMSize(Message);

    AdjustLastColumnAutoSize;
end;

{-----------------------------------------------------------------------------}

procedure TMyGrid.AdjustLastColumnAutoSize;
var
    CurrentColumn : Integer;
begin
    if Assigned(ActiveView) and (ActiveView.Columns.Count > 0) then
    begin
        for CurrentColumn := 0 to ActiveView.Columns.Count - 1 do
        begin
            if ActiveView.Columns[CurrentColumn].Position = ActiveView.Columns.Count - 1 then
            begin
                if ActiveView.Columns[CurrentColumn].Width < ActiveView.Columns[CurrentColumn].FixedWidth then
                    ActiveView.Columns[CurrentColumn].AutoSize := False;

                if ActiveView.Columns.ClientWidth > ActiveView.Columns.FixedWidth then
                    ActiveView.Columns[CurrentColumn].AutoSize := True;

                Break;
            end;
        end;
    end;
end;


#3 Boki (Berg)

Boki (Berg)

    Boki (Berg)

  • Forum Admin
  • PipPipPipPipPip
  • 8,191 posts
  • Gender:Male

Posted 01 April 2023 - 05:11 PM

Hello,

 

Please sorry for delay with the answer.

 

There is already MinWidth property of the column that is used when there is not enough space to apply auto-size anymore. Maybe there is a bug and I will test it a little bit so I'm sure that it's working properly. If not I will fix it for the upcoming release.


boki@bergsoft.net | LinkedIn Profile
--
BergSoft Home Page: www.bergsoft.net
Users Section: users.bergsoft.net
Articles and Tutorials: help.bergsoft.net (Developers Network)
--
BergSoft Facebook page
--
Send us applications made with our components and we will submit them on: www.bergsoft.net/apps.htm. Link to this page will be also set on home page too.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users