VB中如何设置MSFLEXGRIDDE的列宽正好可以显示字段值?

VB中如何设置MSFLEXGRIDDE的列宽正好可以显示字段值?
2025-04-05 23:13:28
推荐回答(1个)
回答1:

把下面的代码放在一个Moudle模块里。
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

使用时这样调用:
Call AdjustColWidth(Form1, MSFlexGridde1, True, 0)

希望对你有帮助和启发^_^