我正在使用从数据库填充的数据表填充列表框。这是相关的代码。
Private Sub frmMainMenu_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'Load data we need from the database
Dim commandCraneType As New OleDbCommand("SELECT Name FROM CraneType WHERE Girders = 'Single Girder'", gConnection)
Dim adapterCraneType As New OleDbDataAdapter(commandCraneType)
Dim dTableCraneType As New DataTable()
'Fill datatables and controls
adapterCraneType.Fill(dTableCraneType)
lboCraneType.DataSource = dTableCraneType
lboCraneType.DisplayMember = "Name"
End Sub
Private Sub lboCraneType_SelectedIndexChanged(sender As Object, e As EventArgs) Handles lboCraneType.SelectedIndexChanged
'Set the gCraneType global variable!
gCraneType = lboCraneType.SelectedItem
Debug.Print(gCraneType)
End Sub
列表框正确地填充了数据库中名称字段中的文本值。但是,当我在列表框中选择该项目时,调试告诉我 gCraneType 已分配 System...DataRowView,并且我收到一个异常,说我无法将该类型转换为字符串。我不明白为什么名称在列表框中正确显示,但选择是一种数据类型。
感谢您提供的任何帮助:)