combobox problem
-
Friday, April 06, 2012 8:50 AM
i have,lets say unexpected problem with combo box.Here is the problem,i set datasource for my combo box and its display wanted column fine,but when i select ex. third member i shows me that i have selected the first one.maybe i didn't explain it very well so here is the code :
strID = ComboBox1.SelectedValue.ToString txtBox.text=strID
summary : it's like i always select first member from combo box collection.if anyone had problem like this please respond .
All Replies
-
Friday, April 06, 2012 9:17 AM
hello:)
Plz check that your ValueMember is set correctly……Sample looks like this:
namespace WinFormCSharp { public partial class Form1 : Form { ComboBox cbox = new ComboBox(); public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { cbox.Parent = this; cbox.Dock = DockStyle.Top; Button btn = new Button(); btn.Parent = this; btn.Dock = DockStyle.Bottom; //Binding things cbox.DataSource = new[] { new { Id = 1, Name = "Name1" }, new { Id = 2, Name = "Name2" } }; cbox.DisplayMember = "Name"; cbox.ValueMember = "Id"; cbox.DropDownStyle = ComboBoxStyle.DropDownList; btn.Text = "Click Me"; btn.Click += new EventHandler(btn_Click); } void btn_Click(object sender, EventArgs e) { MessageBox.Show(cbox.SelectedValue.ToString()); } } }- Marked As Answer by Bob Wu-MTMicrosoft Contingent Staff, Moderator Monday, April 09, 2012 2:34 AM
-
Friday, April 06, 2012 1:27 PM
i have,lets say unexpected problem with combo box.Here is the problem,i set datasource for my combo box and its display wanted column fine,but when i select ex. third member i shows me that i have selected the first one.maybe i didn't explain it very well so here is the code :
strID = ComboBox1.SelectedValue.ToString txtBox.text=strID
SelectedValue is ONLY used when you define "ValueMember" to comboBox. This is in case when you have for example Name and ID columns, Name is DisplayMember (this is actually displayed in comboBox - what you see), and when ID is a ValueMember (you dont see it, but you can use it in your code).
So if you didnt set these 2 properties, you cannot use SelectedValue, BUT only SelectedItem. So in your case, simply replace SelectedValue with SelectedItem and add ToString() method in the end.
Simply, isn`t it?
Mitja
- Marked As Answer by ArminXLX Saturday, April 07, 2012 3:44 PM
-
Friday, April 06, 2012 6:05 PMthanks a lot... :)
-
Friday, April 06, 2012 6:50 PM
You are welcome.
So have you salved the problem? Is it working now?
Mitja
-
Saturday, April 07, 2012 5:50 AM
Yes, i solved it.
Works fine now.
-
Saturday, April 07, 2012 5:51 AM
If possible,plz mark us answers……:-)Yes, i solved it.
Works fine now.
-
Sunday, April 08, 2012 1:20 AM
thanks a lot... :)
You should also mark mine as an answer……:)



