Populate a drop down list with a blank entry

K

KatMagic

How can I populate a drop down list with a blank entry so that it is the
first item in the list?

I tried:
ddl.Items.Add(New ListItem(" ", 0))

And then getting a dataset and binding it to the ddl, which of course loses
the blank item. If I run the above after binding the dataset, the blank
entry is at the end of the list. The problem with that is in several places
in my application, I select an item in the drop down list according to a
value. If the value is not found, it returns 0. If I select the drop down
list according to that 0, it displays the first item in the list, which is
incorrect. If I check for a 0 value, then select the blank entry if it's
zero, then the value that actually does point to the first item isn't found.

Hope that makes sense. Basically, my problem would be solved if I could add
a blank entry first.
 
K

KatMagic

Thanks! Actually, it's ddl.Items.Insert(0, string.empty) instead of
InsertAt, but you got me in the right direction.

Alvin Bruney said:
ddl.Items.InsertAt(0, string.empty)


--

________________________
Warm regards,
Alvin Bruney [MVP ASP.NET]

[Shameless Author plug]
Professional VSTO.NET - Wrox/Wiley
The O.W.C. Black Book with .NET
www.lulu.com/owc, Amazon
Blog: http://www.msmvps.com/blogs/alvin
-------------------------------------------------------

KatMagic said:
How can I populate a drop down list with a blank entry so that it is the
first item in the list?

I tried:
ddl.Items.Add(New ListItem(" ", 0))

And then getting a dataset and binding it to the ddl, which of course
loses the blank item. If I run the above after binding the dataset, the
blank entry is at the end of the list. The problem with that is in
several places in my application, I select an item in the drop down list
according to a value. If the value is not found, it returns 0. If I
select the drop down list according to that 0, it displays the first item
in the list, which is incorrect. If I check for a 0 value, then select
the blank entry if it's zero, then the value that actually does point to
the first item isn't found.

Hope that makes sense. Basically, my problem would be solved if I could
add a blank entry first.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top