?
Solved

vb.net - add an item to a dropdownlist

Posted on 2011-10-14
8
Medium Priority
?
218 Views
Last Modified: 2012-05-12
this dropdown list is being populated from a query from the db..but I want to add 'All' to the top of it...so that when 'ALL'  is selected an event will be trggered

reads in the DDL:

'John Smith'
'Eddie VanHalen'
'Jack Rummer'
'Mary Poppins'

now I want to add:

'ALL'
'John Smith'
'Eddie VanHalen'
'Jack Rummer'
'Mary Poppins'




heres how the DDL is being populated....
dsUser.Tables(0).Columns.Add("FullName", GetType(String), "FirstName + ' ' + LastName")

            ' convert to dataview and filter on qualanex users
            Dim dv As DataView = New DataView(dsUser.Tables(0))
            dv.RowFilter = "ClientCode = 0"

            ddlUser.DataSource = dv
            ddlUser.DataBind()

Open in new window

0
Comment
Question by:GlobaLevel
  • 5
  • 2
8 Comments
 
LVL 17

Accepted Solution

by:
Carlos Villegas earned 2000 total points
ID: 36971302
Try this:
        dsUser.Tables(0).Columns.Add("FullName", GetType(String), "FirstName + ' ' + LastName")

        ' convert to dataview and filter on qualanex users
        Dim dv As DataView = New DataView(dsUser.Tables(0))
        dv.RowFilter = "ClientCode = 0"
        ddlUser.AppendDataBoundItems = True
        ddlUser.DataSource = dv
        ddlUser.DataBind()
        ddlUser.Items.Insert(0, New ListItem("ALL", "-1"))

Open in new window

0
 
LVL 17

Expert Comment

by:Carlos Villegas
ID: 36971322
The AppendDataBoundItems property is not required, just try:
        dsUser.Tables(0).Columns.Add("FullName", GetType(String), "FirstName + ' ' + LastName")

        ' convert to dataview and filter on qualanex users
        Dim dv As DataView = New DataView(dsUser.Tables(0))
        dv.RowFilter = "ClientCode = 0"
 
        ddlUser.DataSource = dv
        ddlUser.DataBind()
        ddlUser.Items.Insert(0, New ListItem("ALL", "-1"))

Open in new window

0
 
LVL 10

Author Comment

by:GlobaLevel
ID: 36971364
your inserting after the databind?...okay
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 17

Expert Comment

by:Carlos Villegas
ID: 36971376
Yes, also you can do it in this way, with the help of AppendDataBoundItems:
ddlUser.Items.Add(New ListItem("ALL", "-1"))
ddlUser.AppendDataBoundItems = True
ddlUser.DataSource = dv
ddlUser.DataBind()

Open in new window

0
 
LVL 17

Expert Comment

by:Carlos Villegas
ID: 36971387
Sorry, again, AppendDataBoundItems is not required, I don't know what happens inside my head right now :p
0
 
LVL 17

Expert Comment

by:Carlos Villegas
ID: 36971390
So just this must work, and it looks more natural:
ddlUser.Items.Add(New ListItem("ALL", "-1"))
ddlUser.DataSource = dv
ddlUser.DataBind()

Open in new window

0
 
LVL 14

Expert Comment

by:nishant joshi
ID: 36976319
 DropDownList1.Items.Insert(0, "All","-1");
0
 
LVL 10

Author Comment

by:GlobaLevel
ID: 36979642
this worked fine after the databind..thanks for the help....
0

Featured Post

Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
Suggested Courses
Course of the Month14 days, 5 hours left to enroll

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question