分组显示数据
为了便于学习本节的内容,可以先打开CaseStudy目录下的文件“ListView.Table”,并参考该文件名为“示例五”的窗口。
本节的目的是设计一个下图所示的窗口,在ListView中用分组的形式显示各国:
设计步骤:
1、在窗口中分别插入一个ListView和Button控件。
2、将按钮的Click事件代码设置为:
Dim
lvw As
WinForm.ListView
= e.Form.Controls("ListView1")
lvw.Groups.Clear()
'清除原来的分组
lvw.Rows.Clear()
'清除原来的行
lvw.Images.Clear()
'清除原来的图片
lvw.View
= ViewMode.LargeIcon
'用大图标模式
Dim
gps() As
String = {"欧洲","亚洲","非洲"}
'定义分组名称
Dim
nms()
As
String
=
{"中国","德国","英国","日本","俄罗斯","法国","埃及","利比亚","赞比亚"}
'定义标题
Dim
ics()
As
String
=
{"China","Germany","UK","Japan","Russia","France","Egypt","Libya","Zambia"}
'定义图标键值
For Each gp
As String In gps
Dim
vg
As
WinForm.ListViewGroup
=
lvw.Groups.Add()
'增加分组
vg.Name
=
gp
'指定分组名称
vg.Text = gp & "国家"
'指定分组标题
Next
For
i As
Integer = 0
To nms.Length
- 1
lvw.Images.AddImage(ics(i),ics(i)
& ".ico",
ics(i)
& "48.ico")
'添加代表这个国家的小图标
Dim r
As WinForm.ListViewRow
= lvw.Rows.Add()
'增加一行
r.Text =
nms(i)
'指定标题
r.ImageKey
= ics(i)
'指定图标键值
Select Case
nms(i)
'指定各个国家所属的分组
Case
"德国","英国","俄罗斯","法国"
r.Group
= "欧洲"
Case
"中国","日本"
r.Group
= "亚洲"
Case
"埃及","利比亚","赞比亚"
r.Group
= "非洲"
End Select
Next
提示:
必须先添加分组到Groups集合中,然后指定行的Group属性,将其分配到相关分组中。