How do your read selected values from a BOUND ListBox in VB.Net

R

Richard Albrecht

I have been trying to figure out for days on how to read values from a Bound
ListBox. The listBox gets the values from an Access Table.

I can read values fine for Non-Bound ListBoxes, But the same code doesn't
work for Bound, see below:

Any one of these work for a non-bound listbox.


Code:
'This get all items
For lnCnt = 0 To lstVolIssue.Items.Count - 1
lsVolIssue &= lstVolIssue.Items(lnCnt) & ","
Next

'These 2 methods get only the selected
For Each lsStr In lstVolIssue.SelectedItems
lsVolIssue &= lsStr.ToString
Next

For lnCnt = 0 To lstVolIssue.SelectedIndices.Count - 1
lsVolIssue = lsVolIssue &
lstVolIssue.Items(lstVolIssue.SelectedIndices.Item(lnCnt)).ToString & ","
Next






When using a BOUND listbox the 1st Code Segment


Code:

'This get all items
For lnCnt = 0 To lstVolIssue.Items.Count - 1
lsVolIssue &= lstVolIssue.Items(lnCnt) & ","
Next




Throws This exception
Cast from type 'DataRowView' to type 'String' is not valid.

The Second Code Segment


Code:

'These 2 methods get only the selected
For Each lsStr In lstVolIssue.SelectedItems
lsVolIssue &= lsStr.ToString
Next




Throws the same exception
Cast from type 'DataRowView' to type 'String' is not valid.

And the third segment


Code:

For lnCnt = 0 To lstVolIssue.SelectedIndices.Count - 1
lsVolIssue = lsVolIssue &
lstVolIssue.Items(lstVolIssue.SelectedIndices.Item(lnCnt)).ToString & ","
Next




Returns
System.Data.DataRowView,System.Data.DataRowView,System.Data.DataRowView,System.Data.DataRowView,

So it has to do with it being bound to a select statement that populates the
list.

HELP!!!

Richard
 
K

Ken Tucker [MVP]

Hi,

The bound list box will return a datarowview. The string you are
looking for is

Directcast(lstVolIssue.SelectedIndices.Item(lnCnt),datarowview).item("YourDisplayMember") Hope that helps.Ken--------------------------"Richard Albrecht" <[email protected]> wrote in messagehave been trying to figure out for days on how to read values from a BoundListBox. The listBox gets the values from an Access Table.I can read values fine for Non-Bound ListBoxes, But the same code doesn'twork for Bound, see below:Any one of these work for a non-bound listbox.Code:'This get all items For lnCnt = 0 To lstVolIssue.Items.Count - 1 lsVolIssue &= lstVolIssue.Items(lnCnt) & "," Next 'These 2 methods get only the selected For Each lsStr In lstVolIssue.SelectedItems lsVolIssue &= lsStr.ToString Next For lnCnt = 0 To lstVolIssue.SelectedIndices.Count - 1 lsVolIssue = lsVolIssue &lstVolIssue.Items(lstVolIssue.SelectedIndices.Item(lnCnt)).ToString & "," NextWhen using a BOUND listbox the 1st Code SegmentCode: 'This get all items For lnCnt = 0 To lstVolIssue.Items.Count - 1 lsVolIssue &= lstVolIssue.Items(lnCnt) & "," NextThrows This exceptionCast from type 'DataRowView' to type 'String' is not valid.The Second Code SegmentCode: 'These 2 methods get only the selected For Each lsStr In lstVolIssue.SelectedItems lsVolIssue &= lsStr.ToString NextThrows the same exceptionCast from type 'DataRowView' to type 'String' is not valid.And the third segmentCode: For lnCnt = 0 To lstVolIssue.SelectedIndices.Count - 1 lsVolIssue = lsVolIssue &lstVolIssue.Items(lstVolIssue.SelectedIndices.Item(lnCnt)).ToString & "," NextReturnsSystem.Data.DataRowView,System.Data.DataRowView,System.Data.DataRowView,System.Data.DataRowView,So it has to do with it being bound to a select statement that populates thelist.HELP!!!Richard
 
R

Richard Albrecht

What is "YourDisplayMember" in:

Directcast(lstVolIssue.SelectedIndices.Item(lnCnt),datarowview).item("YourDisplayMember")

Thanks
 
K

Ken Tucker [MVP]

Hi,

The field you are displaying in the listbox

Ken
------------------------
What is "YourDisplayMember" in:

Directcast(lstVolIssue.SelectedIndices.Item(lnCnt),datarowview).item("YourDisplayMember")

Thanks
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top