How do you have vb automatically size a msflexgrid to the longest field in the record extracted from the database?
does anyone know?
Printable View
How do you have vb automatically size a msflexgrid to the longest field in the record extracted from the database?
does anyone know?
Here is a Generic routine I've put together a while back:
Then just call this routine like this:Code:Public Sub ResizeGrid(pGrid As MSFlexGrid, pForm As Form)
Dim intRow As Integer
Dim intCol As Integer
With pGrid
For intCol = 0 To .Cols - 1
For intRow = 0 To .Rows - 1
If .ColWidth(intCol) < pForm.TextWidth(.TextMatrix(intRow, intCol)) + 100 Then
.ColWidth(intCol) = pForm.TextWidth(.TextMatrix(intRow, intCol)) + 100
End If
Next
Next
End With
End Sub
ResizeGrid MSFlexGrid1, Form1
I'm not sure if this will help you, but following is a procedure I use to set the columns of a grid (in this case the DBGrid control) to the optimum size required to display the longest piece of data in any grid column on the currently shown rows of the grid. Hope it helps.
Lew.
PS: Sorry about the lack of indentation - it's in the original but I'm not sure if it will be there when you view it.
Public Sub Fit_Columns()
Dim i As Integer, j As Integer, L As Integer
Dim CWidths() As Integer
Dim BM As Variant
Screen.MousePointer = vbHourglass
ReDim CWidths(DBGrid1.Columns.Count)
If DBGrid1.VisibleRows > 0 Then
BM = DBGrid1.RowBookmark(0)
'NOTE: The next 8 lines of code are SUPER SLOW! I blame the DBGrid control
For i = 0 To DBGrid1.VisibleRows - 1
Data1.Recordset.Bookmark = DBGrid1.RowBookmark(i)
For j = 0 To DBGrid1.Columns.Count - 1
'The line *** below was replaced because of its slowness in
'retrieving DBGrid1.Columns(j).Text (fault with Apex DBGrid)
'L = TextWidth(DBGrid1.Columns(j).Text) ***
If Not IsNull(Data1.Recordset.Fields(j)) Then
If TextWidth(Data1.Recordset.Fields(j)) <= DBGrid1.Width Then
L = TextWidth(Data1.Recordset.Fields(j))
Else
L = DBGrid1.Width
End If
If L > CWidths(j) Then CWidths(j) = L
End If
Next j
If i < DBGrid1.VisibleRows - 1 Then DoEvents
Next i
Data1.Recordset.Bookmark = BM
For j = 0 To DBGrid1.Columns.Count - 1
If CWidths(j) < 200 Then CWidths(j) = 200
DBGrid1.Columns(j).Width = CWidths(j) + 100
Next j
End If
End Sub