Home > Software engineering >  Vb datagrid automatically adjust the column width
Vb datagrid automatically adjust the column width

Time:09-28

HTML code is as follows, should be quite many people are doing it,

Public Function AutoFitWidth (ByRef dg As DataGrid) As Boolean

Dim rsData As ADODB. You
If rsData Is Nothing Then the Exit Function
If rsData. State=0 Then the Exit Function
If rsData. RecordCount=0 Then the Exit Function
Dim celltext As String
Dim celltext1 As String
'Dim Maxwidth As Single


Dim m As an Integer, I, As an Integer
Dim width As Single
'Maxwidth=0

For m=0 To rsData Fields. The Count - 1
'celltext=dg. The Columns (m). The Text
Celltext=dgRef. Columns (m) Caption
Width=dgRef. Parent. TextWidth (celltext) 'the width of the column header
The first record rsData. MoveFirst '
For I=0 To rsData. RecordCount - 1
Celltext1=rsData. Fields (m)
If dgRef. Parent. TextWidth (celltext1) & gt; Width Then 'biggest field length, is itself are size
Width=dgRef. Parent. TextWidth (celltext1)
End the If
RsData. MoveNext
Next I

DgRef. Columns (m) width=width + 200 'then assignment, set to maximum width
Next m 'move to the second field

End Function
  • Related