Click here to Skip to main content
15,891,184 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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 :
VB
'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:
VB
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
Posted
Updated 31-Jan-18 8:36am

1 solution

Try this:
VB
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)
 
Share this answer
 
Comments
WinterPrison 31-Jan-18 22:30pm    
Thank you Mr.A_Griffin it help me a lot!!! thank you!!!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900