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

老有关问题,新手问。请问MSHFlexGrid分页。小弟我用的是Access数据库的。请看源码,帮助增加

2012-01-21 
老问题,新手问。请教MSHFlexGrid分页。我用的是Access数据库的。请看源码,帮助增加全部只有40分了。望谅解。以

老问题,新手问。请教MSHFlexGrid分页。我用的是Access数据库的。请看源码,帮助增加
全部只有40分了。望谅解。
以下是全部代码。

VB code
Option ExplicitPrivate Const c_bh = 0Private Const c_xm = 1Private Const c_xb = 2Private Const c_nl = 3Private Const c_dh = 4Private Const c_dz = 5Private Const c_tu = 6Private Sub cmdDelete_Click()Dim sql As String    MSHFlexGrid1.Col = c_bh    If MSHFlexGrid1.Text <> "" Then        sql = "delete from 通讯录 where 号码=" & MSHFlexGrid1.Text        conn.Execute sql        ReadDataToGrid    End IfEnd SubPrivate Sub Command2_Click()Call ReadDataToGrid '加载数据End SubPrivate Sub Form_Load()    connectToDatabase    Call ShowOrHideAdmin    Call initGrid   'Call ReadDataToGridEnd Sub'初始化表格Private Sub initGrid()Dim formatString As String    formatString = "^号码|^姓名|^性别|^年龄|^电话|^地址|<照片"    MSHFlexGrid1.formatString = formatString    MSHFlexGrid1.ColWidth(c_bh) = 500    MSHFlexGrid1.ColWidth(c_xm) = 1200    MSHFlexGrid1.ColWidth(c_xb) = 1000    MSHFlexGrid1.ColWidth(c_nl) = 500    MSHFlexGrid1.ColWidth(c_dh) = 600    MSHFlexGrid1.ColWidth(c_dz) = 1000    MSHFlexGrid1.ColWidth(c_tu) = 1250    MSHFlexGrid1.rows = 2    'mshflexgrid1.RowHeight(1) = 2000End Sub'读取数据到表格Private Sub ReadDataToGrid(Optional sqlWhere As String = "")Dim rst As New ADODB.RecordsetDim sql As StringDim i As Long    sql = "select * from 通讯录 where 1=1 " & sqlWhere & " order by 号码"    rst.CursorLocation = adUseClient    rst.Open sql, conn, adOpenForwardOnly, adLockReadOnly        MSHFlexGrid1.rows = 1    MSHFlexGrid1.rows = 2    MSHFlexGrid1.FixedRows = 1    MSHFlexGrid1.Redraw = False    If rst.RecordCount > 0 Then        MSHFlexGrid1.rows = rst.RecordCount + 1        i = 1        Do Until (rst.EOF Or rst.BOF)                MSHFlexGrid1.RowHeight(i) = 400            MSHFlexGrid1.Row = i            MSHFlexGrid1.Col = c_bh: MSHFlexGrid1.Text = myTrim(rst.Fields("号码"))            MSHFlexGrid1.Col = c_xm: MSHFlexGrid1.Text = myTrim(rst.Fields("姓名"))            MSHFlexGrid1.Col = c_xb: MSHFlexGrid1.Text = myTrim(rst.Fields("性别"))            MSHFlexGrid1.Col = c_nl: MSHFlexGrid1.Text = myTrim(rst.Fields("年龄"))            MSHFlexGrid1.Col = c_dh: MSHFlexGrid1.Text = myTrim(rst.Fields("电话"))            MSHFlexGrid1.Col = c_dz: MSHFlexGrid1.Text = myTrim(rst.Fields("地址"))            MSHFlexGrid1.Col = c_tu: MSHFlexGrid1.Text = ""                        Set MSHFlexGrid1.CellPicture = _            LoadPicture("C:\Documents and Settings\Administrator\桌面\C1_Login_Btn.bmp")            rst.MoveNext            i = i + 1        Loop    End If    MSHFlexGrid1.Redraw = True        rst.Close    Set rst = NothingEnd SubPrivate Function myTrim(ByVal strIn As Variant) As String    If IsNull(strIn) Then        myTrim = ""        Exit Function    End If    myTrim = Trim(strIn)End FunctionPrivate Sub ShowPhoto(rf As ADODB.Field)Dim Chunk() As ByteConst ChunkSize As Integer = 2384Dim DataFile As Integer, Chunks, Fragment As IntegerDim MediaTemp As StringDim lngOffset, lngTotalSize As LongDim i As Integer    MediaTemp = App.Path & "\picturetemp.tmp"    DataFile = 1    Open MediaTemp For Binary Access Write As DataFile    lngTotalSize = rf.ActualSize    Chunks = lngTotalSize \ ChunkSize    Fragment = lngTotalSize Mod ChunkSize    ReDim Chunk(Fragment)    Chunk() = rf.GetChunk(Fragment)    Put DataFile, , Chunk()    For i = 1 To Chunks        ReDim Chunk(ChunkSize)        Chunk() = rf.GetChunk(ChunkSize)        Put DataFile, , Chunk()    Next i    Close DataFile    Picture1.Picture = LoadPicture(MediaTemp)    Kill MediaTempEnd SubPrivate Sub mshflexgrid1_Click()Dim rst As New ADODB.RecordsetDim sql As String    MSHFlexGrid1.Col = c_bh    If MSHFlexGrid1.Text <> "" Then        sql = "select * from 通讯录 where 号码=" & MSHFlexGrid1.Text        rst.Open sql, conn, adOpenStatic, adLockOptimistic        If rst.RecordCount <> 0 Then        Text1.Text = (rst.Fields("地址"))            If Not IsNull(rst.Fields("照片")) Then                ShowPhoto rst.Fields("照片")            Else                Picture1.Picture = LoadPicture("")            End If        End If    End IfEnd Sub 



[解决办法]
分页采用adodb Recordset的分页功能。

[解决办法]
利用记录集的pagesize属性,设定好每页显示的数量,就可以实现分页了。
VB code
'例子参考,每页显示50个,控件MSHFlexGridDim   cn   As   ADODB.Connection Dim   rs   As   ADODB.Recordset Dim   page Private   Sub   data()   '显示当前页的内容   Dim   i   Dim   j   i   =   1   Do   While   Not   rs.EOF   And   i   <=   rs.PageSize     dataflex.Row   =   i     dataflex.Col   =   0     dataflex.Text   =   i     For   j   =   0   To   rs.Fields.Count   -   1       dataflex.Col   =   j   +   1       If   IsNull(rs.Fields(j).Value)   Then           dataflex.Text   =   " "       Else         dataflex.Text   =   rs.Fields(j).Value       End   If     Next   rs.MoveNext   i   =   i   +   1   Loop End   Sub Private   Sub   Command1_Click()     '下一页 If   page   =   rs.PageCount   Then   page   =   1 Else   page   =   page   +   1 End   If   data   End   Sub Private   Sub   Command2_Click()   '上一页 If   page   =   1   Then   page   =   1 Else   page   =   page   -   1 End   If     data End   Sub Private   Sub   Form_Load() Dim   j page   =   1 Set   cn   =   New   ADODB.Connection cn.ConnectionString   =   "Provider=OraOLEDB.Oracle.1;Password=mod1;Persist   Security   Info=True;User   ID=mod;Data   Source=sesl1db " cn.Open Set   rs   =   New   ADODB.Recordset rs.ActiveConnection   =   cn rs.CursorType   =   adOpenKeyset rs.LockType   =   adLockPessimistic rs.Source   =   "select   *   from   web_ui_user " rs.Open rs.PageSize   =   50 rs.AbsolutePage   =   page dataflex.Cols   =   rs.Fields.Count   +   1 dataflex.Rows   =   rs.PageSize   +   1     dataflex.Row   =   0   dataflex.Col   =   0     For   j   =   0   To   rs.Fields.Count   -   1     dataflex.Col   =   j   +   1     dataflex.Text   =   rs.Fields(j).Name   Next data End   Sub 

热点排行