?
Solved

winforms listbox - getting the selected value and converting it to int

Posted on 2005-05-12
10
Medium Priority
?
623 Views
Last Modified: 2010-04-16
I cannot seem to convert the SelectedValue object to an INT.

I have tried a bunch of different things, for example:
int id = Int32.Parse((string)listUsers.SelectedValue.ToString());


SHould i be doing something with the DataRowView?


THanks,
Jordan

0
Comment
Question by:jrmcdona
  • 3
  • 3
  • 2
  • +1
10 Comments
 
LVL 9

Expert Comment

by:cyberdevil67
ID: 13992183
Hi jrmcdona,

 You should be using this listBox1.SelectedIndex

Cheers!
0
 
LVL 21

Expert Comment

by:Yurich
ID: 13992287
or like that?
int id = Convert.ToInt32( listUsers.SelectedItem );

regards,
Yurich
0
 
LVL 13

Expert Comment

by:dungla
ID: 13992516
Make sure that you bind an int object to your list item, then SelectedValue will return int object -> you can cast it to int
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 21

Expert Comment

by:Yurich
ID: 13992530
well, binding is good only for web-forms, isn't it?
0
 
LVL 13

Expert Comment

by:dungla
ID: 13992555
Nope, binding is good for winforms too.
0
 

Author Comment

by:jrmcdona
ID: 13992766
none of that is working for this.

it is returning an object  but when i try to do a covert to int, it says the input string is not correct.  I saw something elsewhere about people having same trouble and they had to do something with the DataRowView?
0
 
LVL 9

Expert Comment

by:cyberdevil67
ID: 13992826
What are you trying to achieve, the data or the position in the Listbox.

If you want the data

 string user = listUsers.Items[listUsers.SelectedIndex];

If you want the position.

 int id = listUsers.SelectedIndex;
0
 

Author Comment

by:jrmcdona
ID: 13992895
i am trying to retrieve the value of the selected item, not the text value, but the ID of the selecteditem, which is the ValueMember property when i bind the listbox.

I am trying to pass that value as an INT into a method.

Not the SelectedIndex value.

thanks!
0
 
LVL 9

Accepted Solution

by:
cyberdevil67 earned 1500 total points
ID: 13993057
Then you should be doing this

listUsers.ValueMember[listUsers.SelectedIndex]
0
 

Author Comment

by:jrmcdona
ID: 13997933
listUsers.ValueMember[listUsers.SelectedIndex] did not work for some reason. It was throwing out of bounds errors even with a hardcoded valid index.


However, this did work.

DataRowView row = (DataRowView)listUsers.SelectedItem;
int id = int.Parse(row["UserID"].ToString());

thanks for the suggestions.
0

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
Stellar Phoenix SQL Database Repair software easily fixes the suspect mode issue of SQL Server database. It is a simple process to bring the database from suspect mode to normal mode. Check out the video and fix the SQL database suspect mode problem.

621 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question