how to insert a value at index 0 of a combobox.

Posted on 2009-04-21
Medium Priority
Last Modified: 2012-05-06
I have a query that I run to fill a combo box. it has a valuemember _recid and displaymember "text"
I want the first value to be valuemember =0 and displaymember to be "Please Select a Title"

How can i insert this new value at cboindex 0
Dim magdt As DataTable = New DataTable
            Dim strQuery As String
            strQuery = "Select A_ID, A_Name from tbl_Art order by 2"
            Dim myadapter As New SqlDataAdapter(strQuery, connstring)
            Me.cboAName.DataSource = magdt
            Me.cboAName.DisplayMember = "A_Name"
            Me.cboAName.ValueMember = "A_ID"

Open in new window

Question by:LeeHopkins

Expert Comment

ID: 24194199
You need to use the Items.Insert() method like so:

Me.cboAName.Items.Insert(0, myCombolistItme)

LVL 70

Accepted Solution

Éric Moreau earned 500 total points
ID: 24194419
I often add them directly to the datasource

strQuery = "Select A_ID, A_Name, 2 as sort from tbl_Art UNION Select 0, 'Please Select a Title', 1 order by Sort, A_Name"
LVL 11

Expert Comment

ID: 24194526
Insert into dataset as

ds.Tables(0).Rows.InsertAt(ds.Tables(0).NewRow, 0)
            ds.Tables(0).Rows(0)(DisplayMember) = Display text
            ds.Tables(0).Rows(0)(Value Member) = 0
LVL 11

Expert Comment

ID: 24194558
magdt.Tables(0).Rows.InsertAt(magdt.Tables(0).NewRow, 0)
            magdt.Tables(0).Rows(0)(A_Name) = "Please Select a Title"
            magdt.Tables(0).Rows(0)(A_ID) = 0

Author Closing Comment

ID: 31572723
i like this method it is very flexable on adding other items

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
When we want to run, execute or repeat a statement multiple times, a loop is necessary. This article covers the two types of loops in Python: the while loop and the for loop.
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
Suggested Courses
Course of the Month14 days, 7 hours left to enroll

807 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