只能用时间跟踪.Dim R As Long Dim C As LongPrivate Sub DataGrid1_Change() Label1.Caption = DataGrid1.Text End SubPrivate Sub DataGrid1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single) Dim Str As String
C = DataGrid1.ColContaining(X) R = DataGrid1.RowContaining(Y) Str = Adodc1.Recordset.Fields(C) Label1.Caption = Str
End SubPrivate Sub Form_Load() Label1.AutoSize = True Timer1.Interval = 50 End SubPrivate Sub Timer1_Timer() Dim CSize As Long
On Error Resume Next
CSize = DataGrid1.Columns(C).Width If Label1.Width > CSize Then DataGrid1.Columns(C).Width = Label1.Width End If End Sub
Dim C As LongPrivate Sub DataGrid1_Change()
Label1.Caption = DataGrid1.Text
End SubPrivate Sub DataGrid1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single) Dim Str As String
C = DataGrid1.ColContaining(X)
R = DataGrid1.RowContaining(Y)
Str = Adodc1.Recordset.Fields(C)
Label1.Caption = Str
End SubPrivate Sub Form_Load()
Label1.AutoSize = True
Timer1.Interval = 50
End SubPrivate Sub Timer1_Timer()
Dim CSize As Long
On Error Resume Next
CSize = DataGrid1.Columns(C).Width
If Label1.Width > CSize Then
DataGrid1.Columns(C).Width = Label1.Width
End If
End Sub
///////////判斷你的文字長度,然後改變colwidth(i),但是依然需要timer監控
这样写就可以DataGrid1.Columns(i).Width=TextWidth("你的单元格里边的字符串")
如果网格的字号是12呢?