Articles   Members Online:
-Article/Tip Search
-News Group Search over 21 Million news group articles.
-Delphi/Pascal
-CBuilder/C++
-C#Builder/C#
-JBuilder/Java
-Kylix
Member Area
-Home
-Account Center
-Top 10 NEW!!
-Submit Article/Tip
-Forums Upgraded!!
-My Articles
-Edit Information
-Login/Logout
-Become a Member
-Why sign up!
-Newsletter
-Chat Online!
-Indexes NEW!!
Employment
-Build your resume
-Find a job
-Post a job
-Resume Search
Contacts
-Contacts
-Feedbacks
-Link to us
-Privacy/Disclaimer
Embarcadero
Visit Embarcadero
Embarcadero Community
JEDI
Links
How to Adjust the column width in a TStringGrid to fit the widest text in a cel Turn on/off line numbers in source code. Switch to Orginial background IDE or DSP color Comment or reply to this aritlce/tip for discussion. Bookmark this article to my favorite article(s). Print this article
04-Nov-02
Category
VCL-General
Language
Delphi 2.x
Views
110
User Rating
No Votes
# Votes
0
Replies
0
Publisher:
DSP, Administrator
Reference URL:
DKB
			Author: Tomas Rutkauskas

In the column of a TStringGrid, how do I assign as ColWidth the widest text inside 
its corresponding cells?

Answer:

You measure the text using the TStringGrid's canvas:
1   
2   procedure SetGridColumnWidths(Grid: TStringGrid; const Columns: array of Integer);
3   const
4     DEFBORDER = 8;
5   var
6     max, temp, i, n: Integer;
7   begin
8     with Grid do
9     begin
10      Canvas.Font := Font;
11      for n := Low(Columns) to High(Columns) do
12      begin
13        max := 0;
14        for i := 0 to RowCount - 1 do
15        begin
16          temp := Canvas.TextWidth(Cells[Columns[n], i]) + DEFBORDER;
17          if temp > max then
18            max := temp;
19        end;
20        if max > 0 then
21          ColWidths[Columns[n]] := max;
22      end;
23    end;
24  end;
25  
26  //Use this like:
27  
28  SetGridColumnWidths(stringgrid1, [1, 4]);


This would adjust the widths of columns 1 and 4 to fit the contents.

			
Vote: How useful do you find this Article/Tip?
Bad Excellent
1 2 3 4 5 6 7 8 9 10

 

Advertisement
Share this page
Advertisement
Download from Google

Copyright © Mendozi Enterprises LLC