how to set DropDownList first record is blank?

How to set DropDownList first record is blank

I know one solution is add one blank record in database, but cannot use this method, anymore solution

thanks!!




Answer this question

how to set DropDownList first record is blank?

  • Mac Jamb

    One technique I've used is to add the record right in the select query in your data source using a union. You have complete freedom over what the additonal data contains such as an "All" or "None" selection (putting the phrase in parens will usually force it to sort to the top).

    Select '(none)' as UserName

    Union

    Select Distinct UserName from MyTable

    Order by UserName


  • Ignatius V Ignatius

    The datsource for the dropdown is a dataset.

    After you have done populated the dataset, probably using a tableadapter you have a dataset. This dataset comprises of datatables which comprise of datarows.

    You can simply add a row to the datatable.

    Ds.tables(0).rows.add(<Values>)

    Where ds is the dataset, which contains only 1 table. This will add a row to the datatable - which is being used for the binding.


  • JoshKorn

    If you add a blank first item to the ddl, then set the AppendDataBoundItems to True (on the DDL properties), then perform your databinding, this should solve your problems (do not bother adding a blank record to your database). I hope this helps.
  • Suthy67

    Sorry, I am beginner.

    Would you please give me some example for how to do this



  • Jehan Badshah

    I set the DDL data source is use SqlDataSource, how to pull to dataset and add blank record



  • swells

    If your using databinding - it has to be in the datasource. You can't do databinding and then simply add a single item to the items collection.

    One way is once you have gotten the dataset from the database - add the the blank record to the dataset and then bind. As the dataset is simply a local copy of the database - this additional record wont be propergated back to the database unless you were to do a update.

    Another is to use the dataset and then manually populate the items collection of the dropdownlist - then prior to adding the database items (as this is a non-bound scenario) You can add a blank item and then use the contents of the dataset to populate the items collection. A slower method but ultimately you are determining the contents of the items in the dropdown.


  • CharissaJB123

    Or, after you set your dataset as the datasource to your dropdown, add a blank value to the dropdown:

    ddDropDown.Items.Insert(0, New ListItem(String.Empty, String.Empty))


  • how to set DropDownList first record is blank?