我的代码陷入困境,我想从ListView,示例表中选择多个值:
号 - |名称------ |姓
1 --------- |特拉维斯------ | Stever
2 --------- |克劳迪奥---- |桑切斯
3 --------- | Wes -------- |样式
对我来说,任何片段都会有所帮助。提前谢谢。
这是我目前的代码:
Try
For Each item As ListViewItem In ListView1.SelectedItems
Dim StudentID = Integer.Parse(item.SubItems(0).Text)
Dim FirstName = item.SubItems(1).Text
Dim LastName = item.SubItems(2).Text
DBConn()
SQLSTR = "INSERT INTO '" & TextBox4.Text & "' (StudentID, FirstName, LastName) VALUES ('" & StudentID & "', '" & FirstName & "', '" & LastName & "') "
alterDB()
MsgBox("Students succesfully added", msgboxtitle)
Next
Catch ex As Exception
End Try
SQLCONN.Close()
但是我当前的代码只在ListView中插入最后点击的条目。任何想法,将不胜感激。谢谢!
答案 0 :(得分:1)
首先,将ListView.MultiSelect设置为true。 这允许用户在列表视图中选择多个项目。
单击该按钮时,您可以迭代所选项目,如下所示:
For Each item As ListViewItem In listView.SelectedItems
Dim number = Integer.Parse(item.SubItems(0).Text)
Dim name = item.SubItems(1).Text
Dim Surname = item.SubItems(2).Text
'Add data to database
Next
LINQ方法:
Dim selectedEntries = From item In listView.SelectedItems
Select New With
{
.number = Integer.Parse(item.SubItems(0).Text),
.name = item.SubItems(1).Text,
.Surname = item.SubItems(2).Text
}