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

Posted on 2009-04-21
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

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

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

    LVL 69

    Accepted Solution

    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

    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

    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

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

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Training Course: Java/J2EE and SOA

    This course will cover both core and advanced Java concepts like Database connectivity, Threads, Exception Handling, Collections, JSP, Servlets, XMLHandling, and more. You'll also learn various Java frameworks like Hibernate and Spring.

    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.
    Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
    Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
    The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

    759 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

    Need Help in Real-Time?

    Connect with top rated Experts

    13 Experts available now in Live!

    Get 1:1 Help Now