Question Placing ComboBox database items to listbox?

YOLT

New member
Joined
Nov 17, 2012
Messages
1
Programming Experience
Beginner
Hello,

This is likely an easy question, but I've never dealt with using a DataBase in VB before.

I am using an Access Database to populate items in my ComboBox. I am trying to make it so when you select an item from the ComboBox, it goes into the ListBox. When I debug and try to add items, the ListBox shows "System.Data.DataRowView" when I don't have the Adapter code included, and a "value cannot be Null" error when it is. The ComboBox is populating items from 4 ToolStrips (Sea, Land, Air, Space), the user clicks the ToolStrip button and the appropriate items display.

My code for the ComboBox is-

VB.NET:
Private Sub cboToys_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles cboToys.SelectedIndexChanged
'Toys CBO       
 'Me.TOYAISLETableAdapter.MaleToys(Me.KillinsdatabaseDataSet.ITEMS)
lstItems.Items.Add(cboToys.SelectedItem)
End Sub

How can I get this to work? Any help would be appreciated!
 
When you bind a DataTable to a ComboBox the items come from the DefaultView. That is a DataView and its items are DataRowViews, which is why that's what you see in the ListBox. You have two options:

1. Just as has been done in the ComboBox, set the DisplayMember of the ListBox to the name of the column that you want displayed. To be honest, I'm not 100% sure that that will work with individual items as opposed to binding a whole DataTable.
2. Instead of using the SelectedItem of the ComboBox, use the Text. That will copy just the text displayed in the control, rather than the DataRowView that contains that text but much more besides.
 
Back
Top