mackao Posted May 8, 2012 Posted May 8, 2012 (edited) Hello geniuses. I have a listbox that is populated like this: connDatabase.Open() Dim pk(0) as DataColumn Dim dt as New DataTable dim da as OleDbDataAdapter Try da = New OleDbDataAdapter("Select ArtNum from Artnumber" , connDatabase) da.Fill(dt) pk(0) = dt.Columns("ArtNum") dt.PrimaryKey = pk listbox1.DataSource = dt listbox1.DisplayMember = "ArtNum" listbox1.ValueMember = "Artnum" Catch End Try That works like a charm. But when I'm trying to get all values from this listbox to another listbox using: For Each item in listbox1.Items form2.listbox2.Items.Add(item) Next I only get "System.Data.DataRowView" in the listbox :mad: Is there a way to get around this problem? Br/Marcus Edited May 8, 2012 by snarfblam Quote
Leaders snarfblam Posted May 8, 2012 Leaders Posted May 8, 2012 It looks like the ListBox isn't directly populated with the data but rather with DataRowView objects which the ListBox queries to get the data. You should start by looking at the DataRowView documentation on MSDN and see if there is a simple property or function you can call that gives you what you need. Your code will probably end up looking something like this For Each item in listbox1.Items Dim data As ???? = DirectCast(item, DataRowView).???? form2.listbox2.Items.Add(data) Next Quote [sIGPIC]e[/sIGPIC]
mackao Posted May 9, 2012 Author Posted May 9, 2012 It looks like the ListBox isn't directly populated with the data but rather with DataRowView objects which the ListBox queries to get the data. You should start by looking at the DataRowView documentation on MSDN and see if there is a simple property or function you can call that gives you what you need. Your code will probably end up looking something like this For Each item in listbox1.Items Dim data As ???? = DirectCast(item, DataRowView).???? form2.listbox2.Items.Add(data) Next Thanks Snarfblam for your help. I ended up with another solution: Instead of populating the listbox directly from a msaccess table I first populate a datagridview. Then I just make a loop thru the datagridview and populate the listbox with the values. For i as integer = 0 to dgv1.BindingContext(dgv1.DataSource, dgv1.DataMember).Count-1 listbox1.Items.Add(dgv1.Item(0, i).value) Next Quote
Recommended Posts