Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

ASP.Net add a default item to a Data bound Dropdownlist

Posted on 2009-12-21
6
Medium Priority
?
505 Views
Last Modified: 2012-08-13
Hi

I have a dropdownlist which is populated via a sql query.  I want to append a line which says something like ---Select.

I have read lots of post saying that I need to ass something like

Dropdownlist1.Items.Add("0",new ListItem(0,"--Select--"))

After the data is bound, but I cant figure out how to do this.

Im using VB.

Can any one help?

Thanks

Brasso
0
Comment
Question by:brasso_42
6 Comments
 
LVL 12

Expert Comment

by:techExtreme
ID: 26098010
Hia, Please follow closely the code I have pasted. You need to create your datasource like the example I have posted in form of data table or dataset.   The trick here is how you place that '--select--' item. Just look into the code its very easy.

For my code to work, create a windows project winform application, place a combobox on the form and use the code.

Happy Coding!
Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim dt As New DataTable()
        dt.Columns.Add("FruitsColumn")

        Dim dr As DataRow = dt.NewRow()
        dr("FruitsColumn") = "Apple"
        dt.Rows.Add(dr)

        dr = dt.NewRow()
        dr("FruitsColumn") = "Pineapple"
        dt.Rows.Add(dr)

        dr = dt.NewRow()
        dr("FruitsColumn") = "Banana"
        dt.Rows.Add(dr)

        'NOW WE ARE GOING TO INSERT 'SELECT FRUIT' AS THE FIRST ELEMENT
        'for that, create a 'SELECT FRUIT' ROW and insert at top of table
        dr = dt.NewRow()
        dr("FruitsColumn") = "--SELECT A FRUIT--"
        dt.Rows.InsertAt(dr, 0)

        ComboBox1.DropDownStyle = ComboBoxStyle.DropDownList
        ComboBox1.ValueMember = "FruitsColumn"
        ComboBox1.DisplayMember = "FruitsColumn"
        ComboBox1.DataSource = dt


    End Sub
End Class

Open in new window

0
 
LVL 12

Expert Comment

by:techExtreme
ID: 26098024
Also I forgot, don't try to insert the item when your combo box is already bound.
You will get "Items collection cannot be modified when the DataSource property is set" error if you try to insert item at 0th index in a combobox already bound using say for example a data table!

Happy Coding!
0
 
LVL 15

Expert Comment

by:rajeeshmca
ID: 26098046
aftre u give the bind() for the dropdown you can do like

Ddl1.DataSource = objDataTable
        Ddl1.DataTextField = "Alm_Name"
        Ddl1.DataValueField = "AlmId"
        Ddl1.DataBind()
        Ddl1.Items.Insert(0, "select")        
        Ddl1.Items(0).Value = "0"        'In c#  Ddl1.Items[0].Value = "0"  
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 41

Accepted Solution

by:
guru_sami earned 2000 total points
ID: 26098420
or you can do it via markup by simply setting property AppendDataBoundItems="true" and adding an item to your ddl.
Check this: http://weblogs.asp.net/scottgu/archive/2006/01/29/436804.aspx
0
 
LVL 3

Expert Comment

by:rajquest
ID: 26099421
gridview.items.insert(0,"Default text")
gridview.items(0).value = "default value"
gridview.selectedindex = 0
0
 
LVL 1

Author Closing Comment

by:brasso_42
ID: 31668621
Simplicity is the key and this worked!!!

Thanks m8

Brasso
0

Featured Post

Technology Partners: 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!

Question has a verified solution.

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

I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Suggested Courses

577 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