Adding a value to the top of a dropdown list

mike55

Contributor
Joined
Mar 26, 2004
Messages
727
Location
Ireland
Hi,

Am selecting a list of groups from my database into a dataset, this dataset is then binded to a dropdown list. What I need to do is to add to the top of the dropdown list a value "Master", any suggestions. I have no problem adding the value to the bottom of the dropdown list. Can anyone make a suggestion.

Mike55
 
If it has to be a bound control, you should handle this in the select statement of the dataadapter that fills the dataset.

I suggest you use a "union select" and put your "Master"-entry in the first select.
 
Once you have bound the dropdown to the dataset try using this :

Visual Basic:
Dim objSelectItem As New ListItem("Item To Add Text", -1)
objSelectItem.Selected = True
Me.DropDownLost.Items.Insert(0, objSelectItem)

This should add a new item at the top of the dropdown list - which has been automatically set to be selected.

This has worked for me using a datareader to populate the list but i havent tested with a dataset - hence the reason i say it 'should' work :)

Good Luck!
 
Robby said:
If you do the Insert after DaytaBind it does work.
But what about the DisplayMember and the ValueMember? How has the object to be formed that you add?
 
The insert method has an index and item parm, so they are handled as other items are within the bound control....

dd1.Items.Insert(int_index, string_item)
 
Sorry folks, back to the start (or better choose my solution). :cool:

What do you think, does the following exception want to tell us?
Cannot modify the Items collection when the DataSource property is set.
I told you, but nobody seemed to believe me. :p ;)
 
Last edited:
I'm not sure how your doing it but it has always worked for me, here's a sample ....

With dd
.DataSource = dv
.DataMember = "SomeTable"
.DataValueField = "Some_id"
.DataTextField = "Some_name"
.DataBind()
.Items.Insert(0, "Some text")
End With
 
Robby said:
.DataBind()
Oops, sorry! When I saw .DataBind(), I realized that the question is about Web components. I was talking about Windows components, because there you definitely can't change the collection of a bound control.

Sorry again, my fault. :-\ :o :o
 
Back
Top