VB将数据显示在ListView中

   最近在学VB,所以把一些东西都记下来,方便以后学习。下面是将数据查处来显示在ListView中 

 Dim ledrst As ADODB.Recordset
  Set ledrst = New ADODB.Recordset
  ledrst.Open sql, g_SqlDBConn, adOpenDynamic, adLockPessimistic
  ListView_Show Me.ListView1, ledrst, False, False  '把查处来的数据集合绑定到listView中
  
  ListView1.ColumnHeaders(1).width = 800   ‘列跟字段对应
  ListView1.ColumnHeaders(2).width = 2000
  ListView1.ColumnHeaders(3).width = 800
  ListView1.ColumnHeaders(4).width = 800
  ListView1.ColumnHeaders(5).width = 800
  ListView1.ColumnHeaders(6).width = 800
  ListView1.ColumnHeaders(7).width = 1200
  ListView1.ColumnHeaders(8).width = 4000

 

'将recordset显示到listview
Public Sub ListView_Show(Lvw As ListView, rs As ADODB.Recordset, Optional isNum As Boolean = False, Optional isCon As Boolean = False) '结束时 recordset 对象关闭,是否进行合同列表第九,十列解密
Dim i As Double
Dim Order As Double
Dim itmX As ListItem   '定义一 个ListView的显示项目。
Dim tempstr As String
Dim tmpSum As Double '结算
'Dim tmpRs As ADODB.Recordset
Dim rc As RECT '定义显示listview区域

'On Error GoTo ErrFillListView
Lvw.View = lvwReport
Lvw.Visible = False
Screen.MousePointer = vbHourglass
Lvw.ListItems.Clear   '清除ListView的内容?

With Lvw.ColumnHeaders
.Clear
If isNum Then
    .Add 1, , "序号 ", 800
Else
    .Add 1, , "序号  ", 1
End If
If rs.EOF And rs.BOF Then
    For i = 0 To rs.Fields.Count - 1
        If rs.Fields(i).Name = "Code" Or rs.Fields(i).Name = "SchID" Then
            .Add i + 2, , rs.Fields(i).Name, 0
        Else
            .Add i + 2, , rs.Fields(i).Name, Len(rs.Fields(i).Name) * 200
        End If
    Next

Else
     For i = 0 To rs.Fields.Count - 1
         If rs.Fields(i).Name = "Code" Or rs.Fields(i).Name = "SchID" Then
            .Add i + 2, , rs.Fields(i).Name, 0
         Else
            .Add i + 2, , rs.Fields(i).Name, Len(rs.Fields(i).Name) * 200
        End If
    Next
End If

End With

If rs.EOF And rs.BOF Then
Screen.MousePointer = vbDefault
Lvw.Visible = True
Exit Sub

你可能感兴趣的:(listview,vb,解密,sql)