我正在从SQL Server 2000数据库处理动态列表视图,我需要在其上添加图像。我已将ImageList
控件添加到表单中,并将ListView
属性设置为LargeIcon
,将SmallImageList
设置为ImageList
。
我不知道如何在listview上添加图片。这是我的代码,我不知道它有什么问题,虽然它显示了数据库中的数据,但只显示了图像:
Public Sub PopulateListView_Images(ByVal lv As ListView, ByVal cmd As SqlCommand, ByVal myImageList As ImageList)
Dim da As New SqlDataAdapter
Dim ds As New DataSet
ds.Reset()
da = New SqlDataAdapter(cmd)
da.Fill(ds)
'Fill Method
lv.Columns.Clear()
lv.Items.Clear()
lv.SmallImageList = myImageList
'Fill Listview
Dim c As DataColumn
For Each c In ds.Tables(0).Columns
'adding names of columns as Listview columns
Dim h As New ColumnHeader
h.Text = c.ColumnName
h.Width = 60
lv.Columns.Add(h)
Next
Dim dt As DataTable = ds.Tables(0)
Dim str(ds.Tables(0).Columns.Count) As String
'adding Datarows as listview Grids
Dim rr As DataRow
For Each rr In dt.Rows
For col As Integer = 0 To ds.Tables(0).Columns.Count - 1
str(col) = rr(col).ToString()
Next
Dim ii As New ListViewItem(str)
lv.Items.Add(ii)
'showing the number of records still added
Next
End Sub
在此代码中,仅显示数据但没有图像。我尝试添加:
lv.Items(0).ImageKey = 0