读书人

这个vsflexgrid的网格引导程序LoadRst

发布时间: 2012-01-08 22:48:50 作者: rapoo

这个vsflexgrid的网格引导程序LoadRsttoGrid!
Public Sub LoadRsttoGrid(fg As VSFlexGrid, rst As ADODB.Recordset, Optional ByVal ListNo As Boolean = True)
Dim lngrow As Long, lngcol As Long
Dim i As Long, j As Long
Dim strItem As String

lngrow = rst.RecordCount
If rst.Fields.Count > fg.Cols Then
lngcol = fg.Cols - 1
Else
lngcol = rst.Fields.Count - 1
End If

With fg
.rows = 1
For i = 1 To lngrow
If ListNo = True Then
strItem = i & Chr(9)
Else
strItem = " "
End If

For j = 0 To lngcol
strItem = strItem & rst(j) & Chr(9)
Next

.AddItem Mid(strItem, 1, Len(strItem) - 1)

rst.movenext
Next
End With

End Sub

[解决办法]
rst.RecordCount 有时不会返回真实的记录数的,
所以应该这样比较好:

Public Sub LoadRsttoGrid(fg As VSFlexGrid, rst As ADODB.Recordset, Optional ByVal ListNo As Boolean = True)
Dim lngrow As Long, lngcol As Long
Dim i As Long, j As Long
Dim strItem As String

lngrow = rst.RecordCount
If rst.Fields.Count > fg.Cols Then
lngcol = fg.Cols - 1
Else
lngcol = rst.Fields.Count - 1


End If

With fg
do until rst.eof
If ListNo = True Then
strItem = i & Chr(9)
Else
strItem = ""
End If

For j = 0 To lngcol
strItem = strItem & rst(j) & Chr(9)
Next

.AddItem Mid(strItem, 1, Len(strItem) - 1)

rst.movenext
loop
End With

End Sub
[解决办法]
additem是vsflexgrid中最慢的方法了
为什么不直接用数据绑定
set fg.datasource=rst

读书人网 >VB

热点推荐