Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6825389
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T22:01:02+00:00 2026-05-26T22:01:02+00:00

I have a procedure to auto-resize a column in a grid to accommodate for

  • 0

I have a procedure to auto-resize a column in a grid to accommodate for the largest string in that column. However when there’s over 2,000 records in the grid, it takes a little too much time. Any tips on speeding this up?

//lstSKU = grid
procedure TfrmExcel.ResizeCol(const ACol: Integer);
var
  M: Integer;
  X: Integer;
  S: String;
  R: TRect;
begin
  M:= 20;
  lstSKU.Canvas.Font.Assign(lstSKU.Font);
  for X:= 1 to lstSKU.RowCount - 1 do begin
    S:= lstSKU.Cells[ACol, X];
    R:= Rect(0, 0, 20, 20);
    DrawText(lstSKU.Canvas.Handle, PChar(S), Length(S), R,
      DT_LEFT or DT_VCENTER or DT_CALCRECT);
    if R.Right > M then
      M:= R.Right;
  end;
  M:= M + 15;
  lstSKU.ColWidths[ACol]:= M;
end;
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T22:01:02+00:00Added an answer on May 26, 2026 at 10:01 pm

    Is this a standard TStringGrid/TDrawGrid?

    You can iterate through using Canvas.TextWidth(S) instead to measure the content width of each cell, save the largest, add any padding, and then set the Grid.ColWidths[Col] := M;. This will trigger a single redraw if needed. (Basically what you’re doing, without repeating the drawing operation 2001 times.)

    procedure TfrmExcel.ResizeCol(const ACol: Integer);
    var
      M, T: Integer;
      X: Integer;
      S: String;
    begin
      M := 20;
    
      for X := 1 to lstSKU.RowCount - 1 do 
      begin
        S:= lstSKU.Cells[ACol, X];
        T := lstSKU.Canvas.TextWidth(S);
        if T > M then
          M := T;
      end;
    
      M := M + 15;
      lstSKU.ColWidths[ACol] := M;
    end;
    

    If you want to set both width and height of the cell to accomodate larger fonts or something, use TextExtent instead of TextWidth; TextExtent returns a TSize, from which you can read Width and Height.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a procedure that is run for a lot of items, skipping over
So if a language provides higher order procedure then I can have procedure that
We have a Procedure in Oracle with a SYS_REFCURSOR output parameter that returns the
I have a strange, sporadic issue. I have stored procedure that returns back 5
In a part of my data warehousing stored procedures, i have a procedure that
I have a stored procedure in SQL Server that returns some data via a
In a database I have less than 200,000 records in a table, and in
I have written a CLR stored procedure that is in an assembly. I have
I have MS SQL Server stored procedure that returns XML (it uses SELECT with
I have a stored procedure that I've mapped in my entity framework model (trying

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.