vb.net - add an item to a dropdownlist

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

LVL 10
GlobaLevelProgrammerAsked:
Who is Participating?
 
Carlos VillegasFull Stack .NET DeveloperCommented:
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
 
Carlos VillegasFull Stack .NET DeveloperCommented:
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
 
GlobaLevelProgrammerAuthor Commented:
your inserting after the databind?...okay
0
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

 
Carlos VillegasFull Stack .NET DeveloperCommented:
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
 
Carlos VillegasFull Stack .NET DeveloperCommented:
Sorry, again, AppendDataBoundItems is not required, I don't know what happens inside my head right now :p
0
 
Carlos VillegasFull Stack .NET DeveloperCommented:
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
 
nishant joshiTechnology Development ConsultantCommented:
 DropDownList1.Items.Insert(0, "All","-1");
0
 
GlobaLevelProgrammerAuthor Commented:
this worked fine after the databind..thanks for the help....
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.