Jump to content

TUnimDBGrid AutoWidth Columns


Wicket

Recommended Posts

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

Link to comment
Share on other sites

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;
Link to comment
Share on other sites

  • 1 month later...

Hi Willem

 

Any idea on how to lock a column/s in a UnimDBGrid? (Mobile)

 

There is a property for the UniDBGrid, but I don’t see one for the mobile grid.

 

Regards

Donald

 

 

Sent from my iPad using Tapatalk

Link to comment
Share on other sites

×
×
  • Create New...