ListBox Problem

  • Thread starter Thread starter Wayne Wengert
  • Start date Start date
W

Wayne Wengert

I am trying to take the item the user selected in a list box and put it in a
string variable. When I execute the code
RegionalName = lstRegional.Items(lstRegional.SelectedIndex)


I get this error:
Additional information: Cast from type 'DataRowView' to type 'String' is not
valid.

The listbox is bound to a dataset so I suspect that is part of the problem.
How can I "take" the selection and put it into a variable?
 
The key thing to remember in .NET is that combo and list
boxes do not store simple values, they store types. So
what you've done in this situation is you've bound a
dataset or a datatable to the listbox. You've set it up
to display one value by setting the DisplayMember
property. At this point you can do two things:
1. Set the ValueMember property (and use the
Listbox.SelectedValue property to retrieve the
information which returns a discreet value) or
2. Perform a ctype on the selected item to convert it to
a datarow and then extract the value you want.

Option 2 is more flexible, but Option 1 is a faster
operation.

Jeff Levinson

Author of "Building Client/Server Applications with
VB.NET: An Example Driven Approach"
 
Back
Top