Jump to content


Photo

TUnimDBGrid AutoWidth Columns

TUnimDBGrid Auto width columns

  • Please log in to reply
2 replies to this topic

#1 dan602

dan602

    Active Member

  • uniGUI Subscriber
  • PipPipPip
  • 87 posts

Posted 04 May 2018 - 08:38 AM

Hi,

 

I have noticed that TUnimDBGrid does not have a 'AutoWidth' (forcefit) columns or and equivalent property.

 

Has anyone got a workaround or a different way of achieving this.

 

In some situations I need to use TUnimDBGrid to display data on different devices - with different screen sizes, and it doesn't look great from a UI point of view when the columns do not take up the whole grid width.

 

Any help appreciated,

 

Thanks

 

Dan.


  • 0

#2 WillemvanZyl

WillemvanZyl

    Member

  • uniGUI Subscriber
  • PipPip
  • 32 posts

Posted 10 May 2018 - 07:05 AM

I wrote this for a GridList, sort of works, but based on the theme and font you use you may need to play around with the font.size in the GetTheTextWidth and width calcs:

procedure FitGridColumns(Grid: TunimDBListGrid; theForm:TUnimForm);
const
  C_Add=3;
var
  ds: TDataSet;
  bm: TBookmark;
  i: Integer;
  w,tw, ShortestLength: Integer;
  a: Array of Integer;

  Function GetTheTextWidth(StrIn:String):Integer;
  var c: TBitmap;
  Begin
     Result := 0;
     c := TBitmap.Create;
     try
       c.Canvas.Font.Name := theForm.Font.Name;
       c.Canvas.Font.Size := theForm.Font.Size+2;
       Result := c.Canvas.TextWidth(StrIn);
     finally
       c.Free;
     end;
  End;

begin
  ds := Grid.DataSource.DataSet;
  if Assigned(ds) then
  begin
    bm := ds.GetBookmark;
    try
      if Grid.Columns.Count = 1 then
      Begin
        Grid.Columns[0].Width := grid.Width;
      End
      Else
      Begin
        if ds.RecordCount > 0 then
        Begin
          ds.First;
          SetLength(a, Grid.Columns.Count);
          while not ds.Eof do
          begin
            for I := 0 to Grid.Columns.Count - 1 do
            begin
              if Assigned(Grid.Columns[i].Field) then
              begin
                tw := GetTheTextWidth(Grid.Columns[i].Title.Caption)+40;
                w :=  GetTheTextWidth(ds.FieldByName(Grid.Columns[i].Field.FieldName).DisplayText)+40;

                if w < tw then
                  w := tw;

                if a[i] < w  then
                   a[i] := w ;
              end;
            end;
            ds.Next;
          end;
          for I := 0 to Grid.Columns.Count - 1 do
          Begin
            Grid.Columns[i].Width := a[i] + C_Add;
          End;
        End
        Else
        Begin
          for I := 0 to Grid.Columns.Count - 1 do
          begin
            Grid.Columns[i].Width := GetTheTextWidth(Grid.Columns[i].Title.Caption)+40;
          end;
        End;
      End;
      ds.GotoBookmark(bm);
    finally
      ds.FreeBookmark(bm);
    end;
  end;
end;

  • 0

#3 dan602

dan602

    Active Member

  • uniGUI Subscriber
  • PipPipPip
  • 87 posts

Posted 16 May 2018 - 04:03 PM

Only just seen this post.

 

Thanks Willem, I will give it a try.


  • 0





Also tagged with one or more of these keywords: TUnimDBGrid, Auto width columns

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users