Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
MSDN Visual Basic Community
 
 
 
 
 
TitleMake a FlexGrid's columns fit its data
KeywordsFlexGrid, fit, autofit, ColWidth
CategoriesControls
 
For each column, examine each cell. Use the form's TextWidth method to see how wide the text in that cell should be. Take the maximum of each cell, add a little extra margin, and set the column's width.
 
' Make the columns fit their data.
Private Sub AutoFitFlexGrid(ByVal flx As MSFlexGrid)
Dim r As Long
Dim C As Long
Dim cell_wid As Single
Dim col_wid As Single

    For C = 0 To flx.Cols - 1
        col_wid = 0
        For r = 0 To flx.Rows - 1
            cell_wid = TextWidth(flx.TextMatrix(r, C))
            If col_wid < cell_wid Then col_wid = cell_wid
        Next r
        flx.ColWidth(C) = col_wid + 120
    Next C
End Sub
 
Note that this method assumes the form uses the same font as the FlexGrid control. In particular, if the FlexGrid's first row is a header row with a bold font, its text will be slightly larger than the TextWidth method says. You can change the form's Font.Bold value while working with that row. If the header entries are relatively small compared to the values beneath them, you can ignore this.
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated