Jump to content
Xtreme .Net Talk

Recommended Posts

Posted (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 by snarfblam
  • Leaders
Posted

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

[sIGPIC]e[/sIGPIC]
Posted
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

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...