Adding a list item to the System.Web.UI.WebControls.DropDownList

  • Thread starter Thread starter Chris
  • Start date Start date
C

Chris

I was wondering if there was any way to add a blank list
item control to the beginning of the
System.Web.UI.WebControls.DropDownList's datasource after
the control's datasource has been specified.

Here's my code right now:

Dim oEmptyOption as New ListItem()

'Assign datasource to delivery date type combobox
cboDeliveryDateType.DataSource = oDsDeliveryDateType.Tables
("DataTable")
cboDeliveryDateType.DataTextField =
oDsDeliveryDateType.Tables("DataTable").Columns.Item
("Display_Name").ToString()
cboDeliveryDateType.DataValueField =
oDsDeliveryDateType.Tables("DataTable").Columns.Item
("Delivery_Date_Type_ID").ToString()
cboDeliveryDateType.DataBind()

'Add empty option field
cboDeliveryDateType.Items.Add(oEmptyOption) 'adds to the
end of the list.

Currently, I am able to add the blank item at the end of
the controls Item's list, and select it as the default
selected item, but it appears as the last item in the drop
down list. I want to have it at the beginning of the list.
Any suggestions would be greatly appreciated.

Thanks,

Chris...
 
Chris:

Try using Insert instead of Add - it takes the index to add the ListItem to,
and the ListItem object you want to add:

cboDeliveryDateType.Items.Insert(0, oEmptyOption)

HTH
 
Back
Top