我是新的在vb.net上使用listview,但我设法显示来自mysql数据库的数据,但不知怎的,我没有错误,但在我的列表视图上发生了一些奇怪的事情。
列表视图中的列加倍,每次我退出并再次重新打开它,但是如果我关闭整个应用程序并再次重新运行它,它会恢复正常并且每次单击退出时再次加倍并打开再次盘点。这是表单加载的代码。
Private Sub main_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Timer1.Enabled = True
Call LVWloader()
Call Locker(True)
Call objLocker(False)
With ListView1.Columns
.Add("Semen ID", 50, HorizontalAlignment.Center)
.Add("Bullname", 150, HorizontalAlignment.Center)
.Add("Breed", 150, HorizontalAlignment.Center)
.Add("Quantity", 50, HorizontalAlignment.Center)
.Add("Location", 50, HorizontalAlignment.Center)
.Add("Date Purchased", 50, HorizontalAlignment.Center)
End With
For i As Integer = 0 To ListView1.Columns.Count - 1
ListView1.Columns(i).Width = -2
Next i
End Sub
这是加载程序代码
Public Sub LVWloader()
Dim myCommand As New MySqlCommand
Dim myReader As MySqlDataReader
Dim conn As MySqlConnection
conn = New MySqlConnection
conn.ConnectionString = "server = localhost;username= root;password= a;database= semenis"
Try
conn.Open()
Catch mali As MySqlException
MsgBox("connot establish connection")
End Try
ListView1.Items.Clear()
With myCommand
.Connection = conn
.CommandText = "Select * from inventory"
End With
myReader = myCommand.ExecuteReader
While myReader.Read()
With ListView1
.Items.Add(myReader(0))
With .Items(.Items.Count - 1).SubItems
.Add(myReader.Item(1))
.Add(myReader.Item(2))
.Add(myReader.Item(3))
.Add(myReader.Item(4))
.Add(myReader.Item(5))
End With
End With
End While
End Sub
我认为代码错误来自.add for listview,也许这是另一种方法吗?
我使用vb.net和mysql作为数据库
感谢,
答案 0 :(得分:2)
它重复的原因是因为您使用表单的Load事件来添加列,每次加载表单时都会调用这些列。您需要在表单设计器中或在表单构造函数中添加列。