fguihen Posted December 15, 2005 Posted December 15, 2005 i have databound a combo box to a column in a table like so: 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: 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? Quote
jccorner Posted December 15, 2005 Posted December 15, 2005 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... 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 Quote Applying computer technology is simply finding the right wrench to pound in the correct screw
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.