Link to home
Start Free TrialLog in
Avatar of StefanD
StefanD

asked on

Autosize columns in an a (hierarchical) flexgrid

Hi all,

I want my Flexgrid to automatically size the columns to the content in the columns when the user double clicks on the separator on the top row of the flexgrid. In other words: same behavior as Excel.

thanks,
Stefan
Avatar of Richie_Simonetti
Richie_Simonetti
Flag of Argentina image

ping...
This snippet is not perfect, there is no specific event for clicking/ dblclicking on the sizing handle for the column, the event will fire when the header row is dblclicked. It also relies on having the same font (inc size) used by default on the form and in the grid. If there are different fonts on the grid then this can be handled as well but requires some slight modification.

Private Sub MSFlexGrid1_DblClick()
   Dim dblWidth As Double
   Dim intCol As Integer
   Dim intRow As Integer
   intRow = MSFlexgrid1.MouseRow
   intCol = MSFlexgrid1.MouseCol
   If intRow = 0 Then 'Clicked on header row
       For intRow = 1 To MSFlexGrid1.Rows - 1
           If Me.TextWidth(MSFlexGrid1.TextMatrix(intRow, intCol)) > dblWidth Then
               dblWidth = Me.TextWidth(MSFlexGrid1.TextMatrix(intRow, intCol))
           End If
       Next
       MSFlexGrid1.ColWidth(intCol) = dblWidth + 100
   End If
End Sub
Avatar of StefanD
StefanD

ASKER

Thanks for your post, but I really need to know whether the user doubleclicked on the sizing handle. The column headers are already used for sorting data in that column.

Stefan
Stefan, this is not what you asked in first place!
Avatar of StefanD

ASKER

I tried to make clear I wanted to know whether a user double clicked on the separator between two columns. I just didn't know that it's called a sizing handle. I thought the 'just like excel' part made things clear, but apperntly it didn't. Sorry.

Stefan

ASKER CERTIFIED SOLUTION
Avatar of TimCottee
TimCottee
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of StefanD

ASKER

Indeed, a working solution. This code does exactly what I was looking for. Thank you very much, you deserved the points.

Stefan.