如何从填充了数据库中数据的列表框中获取所选值 [英] How to get the selected value from a listbox populated with data from database

查看:88
本文介绍了如何从填充了数据库中数据的列表框中获取所选值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将填充了数据库的列表框中的所选项目添加到另一个列表框但是当我选择一个项目并将其添加到另一个列表框时,这是另一个列表框显示的内容(System.Data.DataRowView)。我如何解决这个问题或问题出在我的代码中。



对不起,如果我的英语不好。



我尝试过:



这是我填充的列表框的代码:

I want to add the selected item from a listbox populated with database to another listbox but when i select an item and add it to the other listbox this is what the other listbox shows (System.Data.DataRowView). How can i fix this or the problem is in my codes.

Sorry if my English is bad.

What I have tried:

Here is my code to the populated listbox :

'Populate the ListBox with personnelName from personnel '
       Try
           myConnection = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Nikko Jaze Fabellon\Documents\ASRASIM.accdb")
           myConnection.Open()
           ds = New DataSet
           tables = ds.Tables
           da = New OleDbDataAdapter("SELECT [FirstName],[LastName] from [Personnel] where [Status] = 'Activated' ", myConnection)
           da.Fill(ds, "Personnel")
           Dim view1 As New DataView(tables(0))
           With personnelList
               .DataSource = ds.Tables("Personnel")
               .DisplayMember = "FirstName"
               .ValueMember = "LastName"
               .SelectedIndex = 0
           End With

           myConnection.Close()

       Catch ex As Exception
           MessageBox.Show(ex.Message)
       End Try





这是我将所选项目添加到其他列表框的代码:



And this is my code to add the selected item to the other listbox:

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
       Dim str As String

       str = personnelList.SelectedItem.ToString()
       ListBox1.Items.Add(str)
   End Sub

推荐答案

试试这个:
Dim str As String
Dim drv As DataRowView = CType(ListBox1.SelectedItem, DataRowView)
str = CStr(drv.Row.Item("FirstName")) & " " & CStr(drv.Row.Item("LastName"))
ListBox2.Items.Add(str)


这篇关于如何从填充了数据库中数据的列表框中获取所选值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆