Welcome to Dream.In.Code
Getting C# Help is Easy!

Join 107,705 C# Programmers for FREE! Ask your question and get quick answers from experts. There are 1,030 online right now! We've got more than 500 tutorials and 2,000 snippets. Join and find out why Dream.In.Code is the #1 programming help community on the internet! Registration is fast and FREE... Join Now!



C# Combo box - retrieving selected value .net2.0

 
Reply to this topicStart new topic

C# Combo box - retrieving selected value .net2.0, How to get selected value from windows combo box c# .net2.0

Chris Evans
post 18 Jun, 2008 - 09:45 AM
Post #1


New D.I.C Head

*
Joined: 22 May, 2008
Posts: 6

Hi

This is driving me round the twist.
All I want to do is from a populated Combo box retrieve the currently selected value so that I can pass it to some other method.

I have a combo box called cmboLetters with values set :
AB
BC
CD
DE

This was set in the collection of the control on the gui in VS2005

I have set in code the first viewable item on the combo box so we are not looking at an empty entry by doing the following:

this.cmboLetters.SelectedIndex = 0;

This loads up fine. However, I cannot get the selectedIndex value. When I say value I do not mean the index.
At index[0] here it should show me "AB". Well that was what I was expecting..... :[

I have tried all of the following and the results are:

cmboLetters.SelectedText = ""
cmboLetters.SelectedText.ToString() = ""
cmboLetters.SelectedItem.ToString(); = "System.Data.DataRowView"
cmboLetters.SelectedValue.ToString(); = "System.Data.DataRowView"
cmboLetters.SelectedValue = "System.Data.DataRowView"


Can anyone help?
User is offlineProfile CardPM

Go to the top of the page


PsychoCoder
post 18 Jun, 2008 - 09:50 AM
Post #2


DIC.Rules == true;

Group Icon
Joined: 26 Jul, 2007
Posts: 7,143



Thanked 50 times

Dream Kudos: 7700

Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, GDI

My Contributions


For the display value use cmboLetters.SelectedItem.Text for the selected value use cmboLetters.SelectedIndex. Hope that helps smile.gif
User is offlineProfile CardPM

Go to the top of the page

Chris Evans
post 19 Jun, 2008 - 01:48 AM
Post #3


New D.I.C Head

*
Joined: 22 May, 2008
Posts: 6

QUOTE(PsychoCoder @ 18 Jun, 2008 - 09:50 AM) *

For the display value use cmboLetters.SelectedItem.Text for the selected value use cmboLetters.SelectedIndex. Hope that helps smile.gif


Thanks for your reply, but there is only combobox.SelectedItem.ToString() available which returns a "System.Data.DataRowView" not the value of the item.

There must be a way t o get the value "AB" from the combo box.
User is offlineProfile CardPM

Go to the top of the page

Chris Evans
post 19 Jun, 2008 - 02:12 AM
Post #4


New D.I.C Head

*
Joined: 22 May, 2008
Posts: 6

QUOTE(Chris Evans @ 19 Jun, 2008 - 01:48 AM) *

QUOTE(PsychoCoder @ 18 Jun, 2008 - 09:50 AM) *

For the display value use cmboLetters.SelectedItem.Text for the selected value use cmboLetters.SelectedIndex. Hope that helps smile.gif


Thanks for your reply, but there is only combobox.SelectedItem.ToString() available which returns a "System.Data.DataRowView" not the value of the item.

There must be a way t o get the value "AB" from the combo box.



I've found a way of doing it. Forget the combo box and use the List box instead. Here you can get the value in the list by doing the following:

lstBox.Text = "AB"

I'm still puzzled by the combo box???!!!!***
User is offlineProfile CardPM

Go to the top of the page

n8wxs
post 19 Jun, 2008 - 07:22 PM
Post #5


New D.I.C Head

*
Joined: 7 Jan, 2008
Posts: 16



Thanked 1 times
My Contributions


csharp

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
string txt;

txt = comboBox1.SelectedItem.ToString();

MessageBox.Show(txt);

comboBox1.SelectedIndex = 1;

txt = comboBox1.SelectedItem.ToString();

MessageBox.Show(txt);
}




Works for me! smile.gif
User is offlineProfile CardPM

Go to the top of the page

Chris Evans
post 26 Jun, 2008 - 06:45 AM
Post #6


New D.I.C Head

*
Joined: 22 May, 2008
Posts: 6

QUOTE(n8wxs @ 19 Jun, 2008 - 07:22 PM) *

csharp

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
string txt;

txt = comboBox1.SelectedItem.ToString();

MessageBox.Show(txt);

comboBox1.SelectedIndex = 1;

txt = comboBox1.SelectedItem.ToString();

MessageBox.Show(txt);
}




Works for me! smile.gif



The difference is I am binding the combox control with a datatable. At That point it's a nightmare. Admittedly I agree if you add items into the combo box without the datatable binding, it works.

Sadly for me it's come round to bite my behind again.
I can't use a list box as I have 100 items and as the combox consatinas the list it would be more functional for the user.

I'm still feeling hopefully.....Fingers and legs are still crossed for a bit of help.

Thanks
smile.gif

If anyone can help with this one it would be very much apreciated.
User is offlineProfile CardPM

Go to the top of the page

OliveOyl3471
post 27 Jun, 2008 - 12:33 AM
Post #7


D.I.C Addict

****
Joined: 11 Jul, 2007
Posts: 571



Thanked 3 times
My Contributions


edit--nevermind. I had the same answer as n8wxs. Sorry.


This post has been edited by OliveOyl3471: 27 Jun, 2008 - 01:42 AM
User is offlineProfile CardPM

Go to the top of the page

OliveOyl3471
post 29 Jun, 2008 - 10:57 AM
Post #8


D.I.C Addict

****
Joined: 11 Jul, 2007
Posts: 571



Thanked 3 times
My Contributions


QUOTE(Chris Evans @ 26 Jun, 2008 - 08:45 AM) *


If anyone can help with this one it would be very much apreciated.


Did you figure it out yet?
PsychoCoder is probably the only one here with the right answer. If I understand it correctly, this is what he means:

Change
cmboLetters.SelectedValue.ToString(); = "System.Data.DataRowView"

To
cmboLetters.SelectedValue = cmboLetters.SelectedIndex;

And add:
cmboLetters.DisplayMember = cmboLetters.SelectedItem.Text;

If that doesn't work, maybe you can figure it out from one of these sites:

http://msdn.microsoft.com/en-us/library/w67sdsex.aspx
http://forums.microsoft.com/MSDN/ShowPost....97&SiteID=1
User is offlineProfile CardPM

Go to the top of the page

seeker2008
post 12 Aug, 2008 - 05:01 PM
Post #9


New D.I.C Head

*
Joined: 12 Aug, 2008
Posts: 1

QUOTE(Chris Evans @ 18 Jun, 2008 - 09:45 AM) *

Hi

This is driving me round the twist.
All I want to do is from a populated Combo box retrieve the currently selected value so that I can pass it to some other method.

I have a combo box called cmboLetters with values set :
AB
BC
CD
DE

This was set in the collection of the control on the gui in VS2005

I have set in code the first viewable item on the combo box so we are not looking at an empty entry by doing the following:

this.cmboLetters.SelectedIndex = 0;

This loads up fine. However, I cannot get the selectedIndex value. When I say value I do not mean the index.
At index[0] here it should show me "AB". Well that was what I was expecting..... :[

I have tried all of the following and the results are:

cmboLetters.SelectedText = ""
cmboLetters.SelectedText.ToString() = ""
cmboLetters.SelectedItem.ToString(); = "System.Data.DataRowView"
cmboLetters.SelectedValue.ToString(); = "System.Data.DataRowView"
cmboLetters.SelectedValue = "System.Data.DataRowView"


Can anyone help?




I know this was posted a long time ago, but I figured someone like me may be searching for an answer. I'm not really an expert (hope I'm understanding the question), but I was having a similar problem. Since I have some experience with web applications, I was expecting the combobox to behave like the dropdownlist, which it doesn't. Anyhow, I got the selected value by using

string myStr = myComboBox.Text;

It makes sense, I suppose, since users can actually type in their own values so a Combobox acts as a Textbox as well.
User is offlineProfile CardPM

Go to the top of the page

eclipsed4utoo
post 13 Aug, 2008 - 05:40 AM
Post #10


D.I.C Head

**
Joined: 21 Mar, 2008
Posts: 97



Thanked 5 times
My Contributions


QUOTE(Chris Evans @ 19 Jun, 2008 - 05:12 AM) *

QUOTE(Chris Evans @ 19 Jun, 2008 - 01:48 AM) *

QUOTE(PsychoCoder @ 18 Jun, 2008 - 09:50 AM) *

For the display value use cmboLetters.SelectedItem.Text for the selected value use cmboLetters.SelectedIndex. Hope that helps smile.gif


Thanks for your reply, but there is only combobox.SelectedItem.ToString() available which returns a "System.Data.DataRowView" not the value of the item.

There must be a way t o get the value "AB" from the combo box.



I've found a way of doing it. Forget the combo box and use the List box instead. Here you can get the value in the list by doing the following:

lstBox.Text = "AB"

I'm still puzzled by the combo box???!!!!***


the same code works for a combobox.

csharp

string txt = comboBox1.Text;
User is offlineProfile CardPM

Go to the top of the page

Fast ReplyReply to this topicStart new topic
Time is now: 8/30/08 02:01AM

Live C# Help!

C# Tutorials

Reference Sheets

C# Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month