Another way, besides Jerry's:
After the DataBining (you may bind the data all at once by calling
Page.DataBindin g() or call DataBinding on each control), you simply insert a
new ListItem at beginning of the list (Index=0):
''Bind data to the dropdownlist first
DropDownList_co mpanies.DataSou rce=theSource
DropDownList_co mpanies.DataBin ding()
Dim NewItem As ListItem = New ListItem("Selec t Company", "0")
DropDownList_co mpanies.Items.I nsert(0, NewItem)
"Jerry C" <je*****@nospam .nospamwrote in message
news:89******** *************** ***********@mic rosoft.com...
Mike
Try
ddlGroup.Items. Clear()
ddlGroup.Items. Add("Select Company")
ddlGroup.Append DataBoundItems = True
'The key is the appendDataBound Items this will allow the added item to
stay
when
the data source is bound to the dropdownlist it works best if you add the
item before the databinding so it is the first item.
'Create your datasource here
ddlGroup.DataSo urce = MyDatasource
ddlGroup.DataBi nd()
--
Jerry
"Mike" wrote:
>In ASP.NET 2.0 I have a dropdownlist populated from a sqldatasource. The
dropdownlist values are successfully populated from a database table. I
would
like to add the value "Select Company" which will be the selected value
when
the page loads. I attempted to use the code below to add "Select Company"
dynamically but it is not added. Only the values from the database are in
the
dropdownlist . I also tried to added it to the dropdownlist collection in
Visual Studio 2005 but "Select Company" was not added to the
dropdownlist .
How do I add a value to the dropdownlist that is not in the database?
Thank
you.
Dim NewItem As ListItem
NewItem = New ListItem("Selec t Company", "0")
DropDownList_c ompanies.Items. Add(NewItem)