Click here to Skip to main content
15,891,905 members
Please Sign up or sign in to vote.
2.33/5 (3 votes)
I want to assign the Text of a data-bound ComboBox. I tried setting the Text, SelectedValue, and SelectedItem properties but none of them seem to work. I would appreciate suggestions and tips on how best to approach this problem.
none of them seem to work means My Work of Assigning Value to TextBox is Not Done By It.
Posted
Updated 18-Jan-11 20:30pm
v4
Comments
Prerak Patel 18-Jan-11 9:16am    
SelectedValue works fine with databound combobox.
Sandeep Mewara 18-Jan-11 9:20am    
Can you elaborate on what do you mean by "none of them work"? What's your expectation/need?

If you mean that you want to provide the contents of the ComboBox dropdown then you need to use the Items property.

C#
myComboBox.Items.Add("Some Text");
myComboBox.Items.Add("Some More Text");


or
C#
string[] items = new string[] { "Some Text", "Some More Text" };
myComboBox.Items.AddRange(items);


If this is not what you want then you will have to edit your question to make your requirements clearer.
 
Share this answer
 
Comments
Nish Nishant 18-Jan-11 9:24am    
I've edited his question to what I believe he's trying to say.
Henry Minute 18-Jan-11 9:26am    
That's some stutter you've developed, Nish. :)
Nish Nishant 18-Jan-11 9:28am    
Oh actually it got deleted but still shows up (except it's now tagged as deleted). Not bad.
Nish Nishant 18-Jan-11 9:28am    
Uhm the first time I posted it, it did not show up despite refreshing multiple times. So I posted again. And now I am trying to delete the first comment and yet it won't get deleted. Weird!
astika 19-Jan-11 2:44am    
Sir,Actually I Want To Display the Value in Combobox When I Open The Form.
I Can Do This By Using The Text Property
cmbobox1.Text="ABC";
But AS My combobox is bound to database the way is not working.so please Suggest me another way to do this.
Thanks in advance.
Hi astika,

ComboBox will display Text property when you select an item. And SelectedValue, SelectedItem will have value(s) when only if you set DataSource, DisplayMember, ValueMember property. Please try:

cbo.DataSource = dataSet;//Or dataTable
cbo.DisplayMember = "FieldDisplayOfDataset";
cbo.ValueMember = "FieldValueOfDataset";


Update:
When you assigned to ComboBox like that, you can change the Text by set SelectedValue. It will be change the Text display on the control.

example:
cbo.SelectedValue = "ValueExistInField";
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900