Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

binding a listbox to a datasource?

Status
Not open for further replies.

amberlynn

Programmer
Dec 18, 2003
502
CA
So I've been told it isn't possible to have a listbox with two columns of items, and was told to use a listview.
I've been reading up on listviews, but I'm a little confused as to how to populate a listview from a dataview...
With the Listbox, I used the code:

lstAmend.DataSource = dvRPA
lstAmend.DisplayMember = "NUM_AMENDMENT"

but this isn't available for listviews.
I need to be able to display a list of items (containing two columns) from a dataview.

Thanks,
amber

 
The ListView has no complex data binding. You will have to add the items programattically.

For example:

Code:
For Each MyDataRow In MyDataTable.Rows
     Dim LVI As New ListViewItem
     LVI.Text = MyDataRow.Item(0)
     LVI.SubItems.Add(MyDataRow.Item(1))
     MyListView.Items.Add(LVI)
Next
 
Thanks for the help.
I'm confused about the variables myDataRow and MyDataTable.
If I have a dataview that consists of my filtered data, how do I declare myDataRow and MyDataTable.

Dim MyDataTable = MyDataView.Table ?

not sure how to get the MyDataRow declared...

Thanks again,
Amber
 
Dim MyDataTable = MyDataView.Table ?
That should work.

not sure how to get the MyDataRow declared...
I'm only using the MyDataRow variable to reference each row in the table inside the for loop, so you don't need to explicitly set it to anything.

But...on the other hand, as this was a general example I provided, your DataView might be filtered...so you MIGHT NOT want every row from it's DataTable. So to loop through the DataRow, I believe you would have to change the type of loop:

Code:
Dim i As Integer
For i = 0 To MyDataView.Count 
     Dim LVI As New ListViewItem
     LVI.Text = MyDataView.Item(i).Item(0)
     LVI.SubItems.Add(MyDataView.Item(i).Item(1))
     MyListView.Items.Add(LVI)
Next
 
Hi again,
I couldn't get this to work, and moved on to other things.
Now I'm back trying to fix this.
If I use the following code (based on the reply I received here):
For i = 0 To dvRPA.Count - 1
Dim LVI As New ListViewItem
LVI.Text = dvRPA.Item(i).Item(3)
LVI.SubItems.Add(dvRPA.Item(i).Item(5))
test1 = dvRPA.Item(i).Item(3).ToString
test2 = dvRPA.Item(i).Item(5).ToString

lstvwAmend.Items.Add(LVI)
Next
test 1 and test 2 show the 2 values I need to add, for each row into my listview, but nothing ever happens...my listview stays empty.
What am I missing here?
Thanks!
amber
 
Is your ListView set up as Details, or LargeIcon, or what?
 
You also have to make sure you have added columns to the ColumnHeaders collection. You do this in the properties in the IDE for the quickest solution. What you want is the columns you will be adding, and to use Details view. You may also want gridlines as well.
 
That did it...I didn't have any columns added.
Such a simple thing :)

Now it works almost all the time, but occasionally I get the following error:

An unhandled exception of type 'System.Reflection.AmbiguousMatchException' occurred in system.dll

Additional information: No accessible overloaded 'ListViewSubItemCollection.Add' can be called without a narrowing conversion.

Which is triggered by this line of code:
LVI.SubItems.Add(dvRPA.Item(i).Item(5))

What would cause this??
Thanks,
amber
 
Well I've figured out what causes this...if the value of dvRPA.Item(i).Item(5) is null in the database I get this error.
I can't figure out how to get around this...I can't seem to get the right syntax for:
if dvRPA.Item(i).Item(5) = null then don't do anything...

amber
 
For Null use
Code:
If IsDbNull(MyExpression) Then....

Alternately, place a filter on your DataView to exclude nulls.
 
HEEEEEEEEEEEEELP!!!

hello dudes...
i cant get a listbox to read a database... wont show the contents.
is it even possible?
what i want to do is to show all the contents on a especific column of a table , example: "desc_maq"

i dont see anything complex about it.. but seems im stuck.
heeelp please!
thanks in advance...
 
I am having the same problem trying to add items to a list view. My problems is I am using a data adapter / dataset. Is there a way I can populate a list view with a dataset? if so, how? I have not idea how.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top