Resolved Two Display Values in Databound Combobox

posix32

Member
Joined
Apr 20, 2009
Messages
13
Programming Experience
1-3
I am having a problem display a first name and last name from another table that the form is bound to. After a lot of research and experimentation I managed to change the combo box from bound at design time to bound at run time.

This displays all the records in the combo box and selects the first record and displays it as it should. All good upto here.

Overall I have used alot of the VS tools to set up this project. However, I found that it was very limited for displaying multiple columns in a combobox. This way of setting up the project the BindingNavigator is created and I perform the necessary CRUD operations straight away. With setting the project up in this way and setting up the combo box in code, the record in the combo box does not change when you navigate. Is there a way to bind the combo box to the bindingsource even though I have already bound this in code to the SelectedValue of the main table?

The code is as follows:

VB.NET:
Private Sub fillBidOwnersComboBox()

        Dim con As New OleDb.OleDbConnection
        Dim dsBidOwners As New DataSet
        Dim cmdBidOwners As New OleDb.OleDbDataAdapter
        Dim strQryBidOwners As String = "SELECT ID, FirstName + ' ' + LastName AS BidOwnerName FROM BidOwners ORDER BY FirstName ASC"
        Dim intBidOwnersRecordCounter As Integer
        Dim BidOwnersArrayList As New ArrayList

        con.ConnectionString = strDataSource

        Try

            con.Open()

        Catch ex As Exception

            MsgBox("An error occurred while trying to connect to the database." & vbCrLf & _
                   "Contact the System Administrator for help.", _
                   MsgBoxStyle.Critical, _
                   "Database Connection Error")
            Exit Sub

        End Try

        Try

            cmdBidOwners = New OleDb.OleDbDataAdapter(strQryBidOwners, con)
            cmdBidOwners.Fill(dsBidOwners, "BidOwner")

            For intBidOwnersRecordCounter = 0 To dsBidOwners.Tables("BidOwner").Rows.Count - 1

                BidOwnersArrayList.Add(New ValueDescriptionPair( _
                                       dsBidOwners.Tables("BidOwner").Rows(intBidOwnersRecordCounter).Item(0), _
                                       dsBidOwners.Tables("BidOwner").Rows(intBidOwnersRecordCounter).Item(1)))

            Next intBidOwnersRecordCounter

            With BidOwnerComboBox

                .DisplayMember = "Description"
                .ValueMember = "Value"
                .DataSource = BidOwnersArrayList
                .DataBindings.Add("SelectedValue", QuotesDataSet, "Quotes.BidOwner")

            End With

        Catch ex As Exception

            MsgBox("An error occurred while trying to retrieve data from the database." & vbCrLf & _
                   "Contact the System Administrator for help.", _
                   MsgBoxStyle.Critical, _
                   "Database Retrieval Error")
            Exit Sub

        Finally

            con.Close()

        End Try


    End Sub

Thank you for your help in advance. :D
 
Last edited:
Found the Solutions Minutes After Posting

I was binding to a dataset and not the bindingsource. Once this was done, it worked. For anyone that needs to know, the old entry has been commented out and the new one put in below.

VB.NET:
With BidOwnerComboBox

                .DisplayMember = "Description":D
                .ValueMember = "Value"
                .DataSource = BidOwnersArrayList
                '.DataBindings.Add("SelectedValue", QuotesDataSet, "Quotes.BidOwner")
                .DataBindings.Add("SelectedValue", QuotesBindingSource, "BidOwner")

            End With
 
Back
Top