Public Sub AutoSizeFlexGrid(flexObject As MSHierarchicalFlexGridLib.MSHFlexGrid) Dim RsFlex As ADODB.Recordset, sngMaxWidth() As Single, sngTextWidth As Single Dim x As Integer, y As Integer
With flexObject If .Cols = 0 Then Exit Sub .Visible = False ReDim sngMaxWidth(.Cols - 1) For y = 0 To .Rows - 1 For x = 0 To .Cols - 1 sngTextWidth = .Parent.TextWidth(Trim(.TextMatrix(y, x))) * 1 If sngMaxWidth(x) < sngTextWidth Then sngMaxWidth(x) = sngTextWidth Next Next For x = 0 To flexObject.Cols - 1 .ColWidth(x) = sngMaxWidth(x) + 120 Next For x = 0 To .Cols - 1 Set RsFlex = flexObject.DataSource If x < RsFlex.Fields.Count And Not (RsFlex.EOF Or RsFlex.BOF) Then Select Case TypeName(RsFlex.Fields(x).Value) Case 'Integer', 'Double', 'Currency', 'Byte', 'Long', 'Decimal', 'Single' .ColAlignment(x) = flexAlignRightCenter Case Else .ColAlignment(x) = flexAlignLeftCenter End Select End If Next .Row = 0 .Col = 0 If .Rows > 1 Then .Row = 1 .ColSel = .Cols - 1 .Visible = True End With End Sub Public Sub AdjustColWidth(frmCur As Form, gridCur As Object, Optional bNullRow As Boolean = True, Optional dblIncWidth As Double = 0) '-------------------------------------------------------------------- '功能: ' 自动调整Grid各列列宽为最合适的宽度 '参数: ' [frmCur].........................................当前工作窗体 ' [gridCur]........................................当前要调整的Grid '-------------------------------------------------------------------- Dim i, j As Integer Dim dblWidth As Double With gridCur For i = 0 To .Cols - 1 dblWidth = 0 If .ColWidth(i) <> 0 Then For j = 0 To .Rows - 1 If frmCur.TextWidth(.TextMatrix(j, i)) > dblWidth Then dblWidth = frmCur.TextWidth(.TextMatrix(j, i)) End If Next .ColWidth(i) = dblWidth + dblIncWidth + 100 End If Next End With
End Sub 最后如果你使用的是VS2003或以上版本,只要用以下方法实现,VB6可以用上面的方法 你可以这样做设置MSHFLEXGRID的AllowUserResizing 属性 为flexResizeColumns 就可以调整列的宽度了,让隐藏的文字显示出来
|