combo box.selectedText keeps giving me an empty string

fguihen

Junior Contributor
Joined
Nov 10, 2003
Messages
248
Location
Eire
i have databound a combo box to a column in a table like so:

Code:
this.comboBox7.DataSource = ds.Tables["treatment_main"];
			this.comboBox7.DisplayMember = "main_cat";


when the value of comboBox7 is changed i want to populate a second combo box.
when i try to grab the value of the combobox7 i cant.
i have placed this code in both the

SelectionChangeCommitted and the SelectionChanged methods, like so:

Code:
ComboBox cb = (ComboBox) sender;
			string d = cb.SelectedText;

the problem is that in when i try to get the selected text in the SelectionChanged method i get this error

"index and length must refer to a location within the string"

when i try to get the selected in the SelectionChanged method, i simply get an empty string. whats the problem here?
 
The SelectedText will not work on bounded comboboxes. It works only if u add the items using Combobbox1.Items.Add().

For Bounded Columns. please set the value member and display member properly

bind the combo like this.

Me.ComboBox1.ValueMember = "id"
Me.ComboBox1.DisplayMember = "name"
Me.ComboBox1.DataSource = dsetUsers.Tables(0)

it is important that u must set the value member and display member before set the data source . otherwise the selectedvalue changed event fire's one more time...

Code:
Public Sub ComboBox1_SelectedValueChaged (byval sender as object, byval e as eventargs)
      Dim id as string = Convert.ToString(Me.ComboBox1.SelectedValue)  ' Contains your selected user's id
      Dim name as string = Convert.ToString(Me.ComboBox1.SelectedText)  ' Contains your selected user's name
End Sub
 
Back
Top