无法获取 VB.NET ListBox 中多个选定项目的文本,因为它的类型为“datarowview”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13533338/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 11:16:56  来源:igfitidea点击:

Cannot get the text of multiple selected items in a VB.NET ListBox because it's of type "datarowview"

vb.netdata-bindinglistboxlistboxitems

提问by user961627

I've got listbox1in VB 2010, which is bound to a data source and displays values from a dataset. I bound it using the Designer - i.e. not via the code. I just selected the datasource in the properties of listbox1.

我有listbox1VB 2010,它绑定到数据源并显示数据集中的值。我使用设计器绑定它 - 即不是通过代码。我刚刚在listbox1.

Now I want to retrieve the selected values. When I leave the listbox as single select, then ListBox1.SelectedValue.ToStringdoes the job - it gives me the text of the selected item.

现在我想检索选定的值。当我将列表框保留为单选时,然后ListBox1.SelectedValue.ToString完成工作 - 它为我提供所选项目的文本。

But I need it to allow multiple selections. This is my code:

但我需要它来允许多项选择。这是我的代码:

    Dim items As ListBox.SelectedObjectCollection
    items = ListBox1.SelectedItems
    For Each i As String In items
        MsgBox(i)
    Next

And this is the error I get:

这是我得到的错误:

Conversion from type 'DataRowView' to type 'String' is not valid.

I've tried a few different ways to get the values of the selected items but there doesn't seem to be any straightforward way to do it. Is it impossible? Is it necessary to declare a new dataset and fill the listbox programmatically or something?

我尝试了几种不同的方法来获取所选项目的值,但似乎没有任何直接的方法可以做到。不可能吗?是否有必要声明一个新的数据集并以编程方式填充列表框?

回答by Steve

For Each drv As DataRowView In ListBox1.SelectedItems
        MessageBox.Show(drv.Item(0).ToString)
    Next

回答by bendataclear

Off the top of my head I think you can do:

我认为你可以这样做:

Dim items As ListBox.SelectedObjectCollection
items = ListBox1.SelectedItems
For Each i As ListViewItem In items
    MsgBox(i.Value.ToString())
Next

回答by A Paul Savage

Check out my question and answer. It allows you to break out the individual entries from a Database into list boxes.

查看我的问题和答案。它允许您将数据库中的单个条目分解为列表框。

Add 2 List Boxes and a text box to a form then copy this code, You will have to substitute your own Server and database etc entries.

将 2 个列表框和一个文本框添加到表单中,然后复制此代码,您必须替换自己的服务器和数据库等条目。

Imports System
Imports System.Windows.Forms
Imports System.Drawing
Imports System.Collections
Imports System.Data.SqlClient

Public Class Form1

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    ' get the data
    Dim SQLConnectionString As String = "Data Source=HL605\RIVWARE;Database=RIVWARE;Integrated Security=true;"
    Dim mySQLConnection As New SqlConnection(SQLConnectionString)

    ' Populate the list box using an array as DataSource. 
    mySQLConnection.Open()
    Dim SQLDataTable As New System.Data.DataTable

    'Create new DataAdapter
    Dim mySQLDataAdapter = New SqlDataAdapter("SELECT * FROM [Rivware].[dbo].[RivetTypes]", mySQLConnection)
    mySQLDataAdapter.Fill(SQLDataTable)
    ListBox1.DataSource = SQLDataTable
    ListBox1.DisplayMember = "RivetType"

    ' remove validation data from list
    Dim Count As Integer
    Dim X As Integer
    'get the column of data to search for
    For Count = 0 To ListBox1.DataSource.Columns.Count - 1
        X = InStr(ListBox1.DataSource.Columns.Item(Count).ToString, "Name")
        If X <> 0 Then Exit For
    Next
    ' now search for any invalid rows, in that column. those containing "---"
    Dim TheTable As DataTable = CType(ListBox1.DataSource, DataTable)
    Dim theRow As DataRow() = TheTable.Select()
    Dim RowNumber As Integer
    For RowNumber = 0 To UBound(theRow) - 1
        If theRow(RowNumber).Item(Count).ToString <> "---" Then
            ' data is OK so transer it to the other listbox
            ListBox2.Items.Add(theRow(RowNumber).Item(Count - 1).ToString)
        End If
    Next

    ListBox2.ClearSelected()
End Sub 'NewNew

Private Sub ListBox2_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListBox2.SelectedIndexChanged
    TextBox1.Text = ListBox2.SelectedItem.ToString()
End Sub
End Class 'ListBoxSample3