Link to home
Start Free TrialLog in
Avatar of BlakeMcKenna
BlakeMcKennaFlag for United States of America

asked on

Trying to load a ComboBox using a DataTable?

I have a simple procedure in which I'm trying to assign a DataSource to a ComboBox DataSource. When I look at how many rows were added to the DataSource, the count is correct, however, when I go to select an entry from the ComboBox, all the entry values are "System.Data.DataRowView". Not sure what is causing this. Here is my code:

    Private Sub LoadNewUsersComboBox()
        Try
            Dim tbl As New DataTable
            Dim row As DataRow

            tbl.Columns.Add("commonName")
            tbl.Columns.Add("SAMAccountName")

            For x As Integer = 0 To arrStcLDAP.Count - 1
                row = tbl.NewRow
                row("commonName") = arrStcLDAP(x).commonName
                row("SAMAccountName") = arrStcLDAP(x).samAcctName
                tbl.Rows.Add(row)
            Next

            cmbUserName.DataSource = Nothing
            cmbUserName.DisplayMember = tbl.Columns(0).ColumnName.ToString
            cmbUserName.ValueMember = tbl.Columns(1).ColumnName.ToString
            cmbUserName.DataSource = tbl
            cmbUserName.SelectedIndex = -1

        Catch ex As Exception
            EH.ErrorMessage = "LoadNewUsersComboBox() - " & ex.Message & "~E"
        End Try

        EH.ProcessMessages(Me, sbr, EH.ErrorMessage)
    End Sub

Open in new window

Avatar of dustock
dustock
Flag of United States of America image

The ColumnName seems to be causing the issue.  Change to this and it should work fine.

            cmbUserName.DisplayMember = tbl.Columns(0).ToString()
            cmbUserName.ValueMember = tbl.Columns(1).ToString()

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of BlakeMcKenna
BlakeMcKenna
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of BlakeMcKenna

ASKER

I found my own problem!