473,387 Members | 1,497 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Assigning ListBox Current Selection to TextBox

Hello,

I'm a novice user having troubles retrieving data from a listbox in VB
Express 2005.

My listbox is populated by a table from an Access database.

I'm trying to assign the current listbox selection to a textbox using:
"Me.txtSelection.Text = CStr(lstUsers.Items(Me.lstUsers.SelectedIndex))"

When running this code I get the following error:

"InvalidCastException was unhandled. Conversion from type'DataRowView' to
type 'String' is not valid."

I'm assuming that this means that I'm trying to return the entire record
rather than a single field in the record. Unfortunately I don't know how to
return just a single field and I couldn't find anything in 'Help'.

Any assistance is greatly appreciated.

Thanks,
Scott
Jan 30 '06 #1
2 6681
Hi,

<sa*****@netta.us> wrote in message news:dr********@enews4.newsguy.com...
Hello,

I'm a novice user having troubles retrieving data from a listbox in VB
Express 2005.

My listbox is populated by a table from an Access database.

I'm trying to assign the current listbox selection to a textbox using:
"Me.txtSelection.Text = CStr(lstUsers.Items(Me.lstUsers.SelectedIndex))"

When running this code I get the following error:

"InvalidCastException was unhandled. Conversion from type'DataRowView' to
type 'String' is not valid."
If you want to get the text from the selected item in a ListBox, you could
use:
Me.txtSelection.Text = Me.lstUsers.Text

I'm assuming that this means that I'm trying to return the entire record
rather than a single field in the record.
True, if you want to access other fields you can use:

Dim drv As DataRowView = DirectCast( lstUsers.SelectedItem, DataRowView)
Me.txtSelection.Text = drv("fieldname").ToString()

Note that if you bind the TextBox to the same BindingSource as the ListBox
it can automatically show the selected listbox text.

HTH,
Greetings

Unfortunately I don't know how to
return just a single field and I couldn't find anything in 'Help'.

Any assistance is greatly appreciated.

Thanks,
Scott

Jan 30 '06 #2

On 30-Jan-2006, "Bart Mermuys" <bm*************@hotmail.com> wrote:
Hi,

<sa*****@netta.us> wrote in message news:dr********@enews4.newsguy.com...
Hello,

I'm a novice user having troubles retrieving data from a listbox in VB
Express 2005.

My listbox is populated by a table from an Access database.

I'm trying to assign the current listbox selection to a textbox using:
"Me.txtSelection.Text = CStr(lstUsers.Items(Me.lstUsers.SelectedIndex))"

When running this code I get the following error:

"InvalidCastException was unhandled. Conversion from type'DataRowView'
to
type 'String' is not valid."


If you want to get the text from the selected item in a ListBox, you could

use:
Me.txtSelection.Text = Me.lstUsers.Text

I'm assuming that this means that I'm trying to return the entire record
rather than a single field in the record.


True, if you want to access other fields you can use:

Dim drv As DataRowView = DirectCast( lstUsers.SelectedItem, DataRowView)
Me.txtSelection.Text = drv("fieldname").ToString()

Note that if you bind the TextBox to the same BindingSource as the ListBox

it can automatically show the selected listbox text.

HTH,
Greetings

Unfortunately I don't know how to
return just a single field and I couldn't find anything in 'Help'.

Any assistance is greatly appreciated.

Thanks,
Scott


You're a genius! It worked great! Thanks for the help.

So much for online help...

Thanks,
Scott
Jan 30 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Annette Massie | last post by:
I would like to have a form that lists current addresses being used. On this form I would also like to have a command button that would allow the user to add an address if they do not see it...
0
by: VbScripter | last post by:
In the code below, I have an editItemTemplate that will have to have a long list of ip's bound to it. The problem is that the lisbox is not seen by asp.net until after the datalist containing it...
1
by: Edward | last post by:
I am having a terrible time getting anything useful out of a listbox on my web form. I am populating it with the results from Postcode lookup software, and it is showing the results fine. What...
3
by: me | last post by:
Is there any particular reason why a requiredfieldvalidator cant validate a listbox? I have bound the two together but the validator always returns false on isvalid and so does the Page, no matter...
3
by: Ali Chambers | last post by:
Hi, I have created a listbox called "dtlist1" on my VB.NET form. I call a procedure as follows: Private Sub openfile(flname As String) dtlist1.Items.Clear() etc..
4
by: Jeff | last post by:
....still new at this. ...hopefully a simple question Is there some practical way of altering the function of a keypress in Visual Web.net 2005 using VB without causing a postback on each...
5
by: CCLeasing | last post by:
Hello, I have searched google but can not find a straight forward answer to my problem. Hopefuly someone will be kind enough to offer their expertise. Please forgive if this seems a bit convoluted...
9
by: zdrakec | last post by:
Hello all: Clearly, I'm not getting it! Here is the scenario: On a web page, I have two list boxen and a text box. The first listbox is populated at page load time (if it is not a postback)....
0
by: rahulnag22 | last post by:
Hi, I am using a listbox in selectmode = MULTIPLE, I can get the current selected item in the listbox from index = "ACTIVE" , but is there a way to convert this "ACTIVE" to get the current...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.