首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > VB >

这个vsflexgrid的网格引导程序LoadRsttoGrid!解决方案

2012-01-08 
这个vsflexgrid的网格引导程序LoadRsttoGrid!PublicSubLoadRsttoGrid(fgAsVSFlexGrid,rstAsADODB.Recordse

这个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

热点排行