Click here to Skip to main content
15,886,724 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
hello
i have a combobox in program and i binded combobox with select query
C#
combogroup.DataSource = CNRL.ShowGroup();
combogroup.DisplayMember = "TitleGroup";
combogroup.ValueMember = "GroupID";

when i select item from combo cant see selected item in combo text
C#
combogroup.text = combogroup.selecteditem.tostring();
Posted
Comments
Sinisa Hajnal 13-Mar-15 4:58am    
What are you trying to do? Setting combo.text to combo.selecteditem.tostring makes no sense since they should be the same.
Member 11046620 13-Mar-15 6:35am    
how can show selected item in combobox.text?
Joan Magnet 13-Mar-15 12:09pm    
Try this way:

combo.Items[combo.SelectedIndex].Text

or

combo.Text
Member 11046620 13-Mar-15 12:35pm    
It was inconclusive :(
Joan Magnet 13-Mar-15 12:41pm    
If your combo has a binding to a data source, probably, there isn't a SelectedItem, there is a SelectedValue o SelectedText. Has SelectedIndex the correct value?

If .ShowGroup() returns a DataTable I suppose every item of your combo is a DataRowView.

The easy way is using a BindingSource control, and using .Current to get the selected item, if not:

First: combo.DropDownStyle = DropDownList


To get displayed text: (I assume you're not interested in .SelectedValue)

DataRowView view = (DataRowView)combo.SelectedItem;
string tx = view.Row["TitleGroup"].ToString();
 
Share this answer
 
If i understand your problem correctly, you want to get the text of the selected item, when a selection changes (?)

I recommend you use the selectedindexchanged event, there is a selectedvaluechange event, but it is for unknown reasons not as reliable.

Here you can use the very nice index (most reliable) in the list to access the individual data

https://msdn.microsoft.com/en-us/library/system.windows.forms.combobox.selectedindexchanged(v=vs.110).aspx[^]

and if you want to access the text of the selected text, it much depends on what you're binding on, as the implementation states that it is an object it could be anything
https://msdn.microsoft.com/en-us/library/system.windows.forms.combobox.selectedindex(v=vs.110).aspx[^]

However a Combobox is a listcontrol and as such it supports two properties "displaymember" and "valuemember", it is the displaymember property you want to if the value "displaymember" is not supplied it will have a value "" and in this case cause a call to the .ToString() method of the object.

So supply the displaymember to your combobox and then cast the item the type you're binding to and access the (property) named like that, this should be dotable after casting.
 
Share this answer
 

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