How do you select the 1st. combo box item in code?

emaduddeen

Well-known member
Joined
May 5, 2010
Messages
171
Location
Lowell, MA & Occasionally Indonesia
Programming Experience
Beginner
Hi Everyone,

I'm trying to use:

VB.NET:
.SelectedIndex = 0

to select the 1st. item of a combo box. Instead of displaying a customer name the combo box shows "System.Data.DataRow"

It works if the user actually selects something in the combo box but I would like to do this in code.

Can you tell me what I'm missing to get the customer name to display in the combo box?

Thanks.

Truly,
Emad
 

Attachments

  • selecting a combobox item.jpg
    selecting a combobox item.jpg
    370.5 KB · Views: 30
It must be something in the way you're setting the datasource (I never used combo-box with datasource)
cause if you do it manually it works just as expected:
VB.NET:
        Month.Items.Clear()
        Dim i As Integer
        For i = 1 To 12
            Month.Items.Add(MonthName(i, True))
        Next i
        Month.SelectedIndex = 0
 
Generally I would tell the control which members to use for display before giving it the data to display.
Though I find it strange it would display ".DataRow", when bound to a DataTable it would display ".DataRowView" when no DisplayMember is set. Is it possible those cells contains DataRow instances and not values?
 
Hi Everyone,

Maybe it's a bug with the control I'm using since it is the PureComponents combo box control.

Also the data is actually from the Northwind sample database.

Here is how I am using the code:

VB.NET:
    ' Declare data adapter and data table for this form.
    '---------------------------------------------------
    Private objDataAdapterCustomers As SqlDataAdapter
    Private objDataTableCustomers As DataTable = New DataTable()

    Private objCurrentDataRow As DataRow

    Private Sub Form_Locate_a_DataTable_Row_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim strSQL As String = "Select * From Customers"
        Dim objDataColumns(1) As DataColumn

        ' Set up the exception catch.
        '----------------------------
        Try

            ' Create the data adapter and fill the data table.
            '-------------------------------------------------
            objDataAdapterCustomers = New SqlDataAdapter(strSQL, _
                                          (BuildConnectionString("EMAD-PC\SQLEXPRESS", "Northwind")))

            objDataAdapterCustomers.Fill(objDataTableCustomers)

            ' This array will hold the primary key "CustomerID". for the data table.
            '-----------------------------------------------------------------------
            objDataColumns(0) = objDataTableCustomers.Columns("CustomerID")
            objDataTableCustomers.PrimaryKey = objDataColumns

            ' Bind the data to the combo box.
            '--------------------------------
            With ComboBoxCustomers
                .DataSource = objDataTableCustomers
                .DisplayMember = "CompanyName"
                .ValueMember = "CustomerID"
                .SelectedIndex = 0
            End With

        Catch exSqlErrors As SqlException
            MsgBox(exSqlErrors.Message)
        End Try

    End Sub

Did I miss a step maybe?

You also mentioned that you tell the control which members to use for display before giving it the data to display. Could you alter my code to do that so I can learn the technique?

Truly,
Emad
 
Last edited:
DisplayMember is where you tell which member the control should display. ValueMember is where you tell the control which member it should get value from. DataSource is where you bind the data.
 
Back
Top