Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Auto-Resize Column Width in Grid

Status
Not open for further replies.

Opieo

Programmer
Jul 26, 2006
454
0
0
GB
Okay, is there any way to get the Column Width for all of the columns in a String Grid to automatically resize to the maximum needed lengths based on what is in the fields?

Kind of like the effect you get in MS Excel or Access when you double click on the line between columns, it auto resizes to maximum needed.

Any way to get the columns to do that after populating the grid?

~
Give a man some fire, he will be warm for a day, Set a man on fire, he will be warm for the rest of his life.
 
Just a thought on this. set up a loop to read down through each columns counting the number of characters in each cell; and keeping the largest counts.

multiply the pixel size of each charater by the maximum number of characters in each cell to get the total number of pixels in a cell, and add a couple for the margins.

Did something similar many years ago. Will have a dig about and see if I can fine some code for you.

All the best,
kinnon
 
Try this out...
Code:
Procedure TForm1.SetColWidth(col:Integer);
var i,str_width, col_width:Integer;
Begin
     { set the width of the defined column in a string grid to the
     width of the widest string }
     str_width:=0;
     col_width:=0;
     for i := 0 to StringGrid1.RowCount - 1 do
     begin
       // get the pixel width of the complete string
       col_width:=Form1.Canvas.TextWidth(StringGrid1.Cells[col,i]);
       // if its greater, then put it in str_width
       if col_width>str_width then str_width:=col_width;
     end;
     StringGrid1.ColWidths[col]:=col_width+5; // +5 for margins. adjust if neccesary
End;
 
That was the only thing I had in mind.
Although it would be much better to use with a fixed width font was what the plan would have to be.
I was just curious if there was an easier way.
And I would probably use the Length function to check every string while I am populating the table itself, and save the time of doing a 2nd loop through.
Again I was just wondering if there was some simple way to auto-size it without manually keeping track of string lengths.
I thank you though.

~
Give a man some fire, he will be warm for a day, Set a man on fire, he will be warm for the rest of his life.
 
ohp!
My bad, I should have read your code more closely.
I see that rather than having to use a fixed width font at all, I could instead use the Text Width property.
Now that, would be more efficient than going through using a length function and then multiplying, not to mention my idea was bound to a fixed width.
Thank you even more!
That is short enough that I will implement it while going through as it does not bind me to any font.
Again though, if there is some simple function to just tell a whole grid's columns to auto-size (which I suspect there isn't?), that would be cool. =)

~
Give a man some fire, he will be warm for a day, Set a man on fire, he will be warm for the rest of his life.
 
np. i dont think ther is an auto size option. another idea to avoid iteration would be to keep an array of widths as a unit variable, and run a compare between the field just edited and the width last recorded. that way you could avoid iterating through all the rows. maybe theres an on cell exit or something you could perform a check with.

haven't looked into it but am sure it could be done.


All the best,
kinnon
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top