populating the dropdown with hard coded values

Posted on 2006-04-24
Last Modified: 2010-04-23
How can I populate my combobox with hard coded values. Please note that what I will display in the selection list is also the value i.e 1 is really 1 and not some value1.
Please include any other steps I have to take like opening the datasource etc as well.

Question by:LuckyLucks
    LVL 6

    Expert Comment


       Public Function FillCombo(ByRef cBox As Object, ByVal Tbl As String, ByVal IdFld As String, ByVal NameFld As String, ByVal SqsString As String) As String

                ' cBox ur ComboBox    
                ' v_ConnectionString Connection String
                ' Tbl  ur table ( Or View ) name
                ' IdFld  DisplayMember Field
                ' NameFld DisplayMember Field
                ' SqsString Select statment

                Dim Conn As SqlConnection = New SqlConnection(v_ConnectionString)
                Dim Comm As SqlCommand = New SqlCommand(SqsString, Conn)
                Comm.Connection = Conn
                Comm.CommandText = SqsString ' "commandtorun"
                Comm.CommandType = CommandType.Text
                Dim DS As DataSet = New DataSet()

                Dim DA As New SqlDataAdapter(Comm)
                DA.Fill(DS, Tbl)

                Dim dt As New DataTable
                dt.Columns.Add(NameFld, GetType(System.String))
                dt.Columns.Add(IdFld, GetType(System.String))

                With cBox
                    .DataSource = DS.Tables(Tbl)    'dt
                    .DisplayMember = NameFld
                    .IdFld = IdFld
                End With

                Call ClossConn(Conn) ' Closs Current Connection

                Return 0
            Catch ex As Exception

                Return ex.Message

            End Try
        End Function

    Author Comment

    querying the database indicates populating the dropdown with dymanic data. I dont have an underlying table. I need a simple drop down for 3 predefined values.

    LVL 34

    Accepted Solution

    1)  In design view you can add harcoded items to the combo by selecting the control and then, in the properties window, clicking on the button under "Items" and typing them in the pane that appears.

    2)  In code, you can use



            Dim myArray(2) As String
            myArray(0) = "ItemA"
            myArray(1) = "ItemB"
            myArray(2) = "ItemC"


    Featured Post

    Find Ransomware Secrets With All-Source Analysis

    Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

    Join & Write a Comment

    Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
    I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
    This video discusses moving either the default database or any database to a new volume.
    Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

    746 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

    20 Experts available now in Live!

    Get 1:1 Help Now