我在vb.net的ListView中显示数据库时遇到问题。它不是错误,但它只显示ListView中的一列。这是我的代码:
MyCommand = New SqlCommand("SELECT *FROM TSiswa WHERE Nama = '" & txtNama.Text & "' ", comm)
adapter = New SqlDataAdapter(MyCommand)
Dim dt As New DataTable
adapter.Fill(dt)
For i As Integer = 0 To dt.Rows.Count - 1
With ListView1.Items.Add(dt.Rows(i)("Nama"))
End With
Next
我也试过这个代码:
With .Items(.Items.Count - 1).SubItems
.Add(dt.Rows(i)("Alamat"))
.Add(dt.Rows(i)("Kelas"))
End With
但它没有帮助。
答案 0 :(得分:0)
尝试使用Do Until循环,以便检查数据适配器中的每一行,并在下面的listviewitem中添加数据库中的其他字段,以显示在listview的每一行中。
MyCommand = New SqlCommand("SELECT *FROM TSiswa WHERE Nama = '" & txtNama.Text & "' ", comm)
adapter = New SqlDataAdapter(MyCommand)
Dim dt As New DataTable
adapter.Fill(dt)
Dim i as integer
Do Until i = dt.Rows.Count
'FIELDS FROM YOUR TABLE
dim lvt as new listviewitem
lvt.subitems.add(dt.Rows(i)("Nama"))
Listview1.items.add(lvt)
i=i+1
Loop