CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    May 2000
    Location
    Belgium, Bruges
    Posts
    146

    MShflexgrid set colwidth so you can see the whole cell content

    Hi,

    How can one set the width so you can see the entire text that is in it in a mshflexgrid (like you have autofit selection in excel)?

    Thanx


  2. #2
    Join Date
    Apr 2002
    Location
    England
    Posts
    66

    Re: MShflexgrid set colwidth so you can see the whole cell content

    Don't think there's an autosize in Flexgrid, but you 'can' do it with the following code:-

    If flxGrid.ColWidth(iColNum) < myForm.Textwidth(sCellText) then
    flxGrid.ColWidth(iColNum) = myForm.Textwidth(sCellText) + 75
    End If




    Where iColNum is the Column Number and sCellText is the text you are resizing the column to. Therefore, if column iColNum has sCellText in one of its rows, the Column will be resized to that sCellText fits.

    I haven't run this code, so if it doesn't work, you'll have to tweak it, but you get the idea.

    Regards,

    Rob

    ------------------------------------------------------------------------------------------------------
    The trouble with doing something right the first time is that no one appreciates how difficult it was.
    If at first you don't succeed, skydiving is not for you.

  3. #3
    Join Date
    May 2000
    Location
    New York, NY, USA
    Posts
    2,878

    Re: MShflexgrid set colwidth so you can see the whole cell content


    ' Inputs:msFG (MSFlexGrid) = The name of the flex grid to resize ....
    'MaxRowsToParse (integer) = The maximum number of rows (depth) of the table to scan for cell
    'width (e.g. 50) .... MaxColWidth (Integer) = The maximum width of any given cell
    'in twips (e.g. 5000)
    '

    public Sub AutosizeGridColumns(byref msFG as MSFlexGrid, byval MaxRowsToParse as Integer, byval MaxColWidth as Integer)
    Dim I, J as Integer
    Dim txtString as string
    Dim intTempWidth, BiggestWidth as Integer
    Dim intRows as Integer
    Const intPadding = 150

    With msFG
    for I = 0 to .Cols - 1
    ' Loops through every column
    .Col = I
    ' set the active colunm
    intRows = .Rows
    ' set the number of rows
    If intRows > MaxRowsToParse then intRows = MaxRowsToParse
    ' If there are more rows of data, reset
    ' intRows to the MaxRowsToParse constant
    '
    intBiggestWidth = 0
    ' Reset some values to 0
    for J = 0 to intRows - 1
    ' check up to MaxRowsToParse # of rows a
    ' nd obtain
    ' the greatest width of the cell contents

    .Row = J

    txtString = .Text
    intTempWidth = TextWidth(txtString) + intPadding
    ' The intPadding constant compensates for text insets
    ' You can adjust this value above as desired.

    If intTempWidth > intBiggestWidth then intBiggestWidth = intTempWidth
    ' Reset intBiggestWidth to the intMaxCol
    ' Width value if necessary
    next J
    .ColWidth(I) = intBiggestWidth
    next I
    ' Now check to see if the columns aren't
    ' as wide as the grid itself.
    ' If not, determine the difference and expand each column proportionately
    ' to fill the grid
    intTempWidth = 0

    for I = 0 to .Cols - 1
    intTempWidth = intTempWidth + .ColWidth(I)
    ' Add up the width of all the columns
    next I

    If intTempWidth < msFG.Width then
    ' Compate the width of the columns to the width of the grid control
    ' and if necessary expand the columns.
    intTempWidth = Fix((msFG.Width - intTempWidth) / .Cols)
    ' Determine the amount od width expansion needed by each column


    for I = 0 to .Cols - 1
    .ColWidth(I) = .ColWidth(I) + intTempWidth
    ' add the necessary width to each column
    '

    next I
    End If
    End With
    End Sub




    Iouri Boutchkine
    [email protected]
    Iouri Boutchkine
    [email protected]

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured