Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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?

Posted

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 

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...