右击窗体上的listview控件,选择属性,将标签编辑的下拉框设为1
或者在vb右侧属性窗口将listview的LabelEdit属性设为1
或者在窗体的加载过程里写一句listview1.LabelEdit = 1
以上三种方法任选其一即可
用下面的参考把
Private Sub Form_Load()
ListView1.View = lvwReport '显示风格
ListView1.Appearance = ccFlat '平面显示
ListView1.BorderStyle = ccNone '外框风格
ListView1.LabelEdit = lvwManual '不允许即时编辑标签
ListView1.MultiSelect = True '允许多选
ListView1.GridLines = True '显示网格线
ListView1.FullRowSelect = True '整行选取
ListView1.FlatScrollBar = False '使用三维的滚动条
ListView1.ColumnHeaders.Clear '清除默认列标头
Dim cn As New ADODB.Connection
Dim rst As New ADODB.Recordset
Dim SqlStr As String
cn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=\\jswnd-Server\DATA\suopei.mdb;Persist Security Info=False"
SqlStr = "Select * From ABC"
rst.CursorLocation = adUseClient
rst.Open SqlStr, cn, adOpenDynamic, adLockOptimistic, adCmdText '打开记录集
'以下为列标头设置
For I = 0 To rst.Fields.Count - 1
If I = 0 Then
ListView1.ColumnHeaders.Add , , rst.Fields(I).Name, ListView1.Width / rst.Fields.Count, lvwColumnLeft ', i + 1 '第一列必须左对齐
Else
ListView1.ColumnHeaders.Add , , rst.Fields(I).Name, ListView1.Width / rst.Fields.Count, lvwColumnCenter ', i + 1
End If
Next I
'以下为添加数据
j = 0
Do While rst.EOF = False
j = j + 1
ListView1.ListItems.Add , , rst.Fields(0).Value '第一列与其它列不一样
For I = 1 To rst.Fields.Count - 1
On Error Resume Next
ListView1.ListItems.Item(j).SubItems(I) = CStr(rst.Fields(I).Value) '其它列
Next I
rst.MoveNext
Loop
End Sub
ListView1.SelectedItems.Clear()就可以了,
可以用下面的语句测试
MsgBox(CStr(ListView1.SelectedItems.Count))
ListView1.SelectedItems.Clear()
MsgBox(CStr(ListView1.SelectedItems.Count))