VB.NET ComboBox Datasource

CipherIS
CipherIS used Ask the Experts™
on
Trying to populate my combobox.  I get an error on "ValueMember".  When I bypass that statement the combobox is filled with "System.Data.DataRow".
    Private _results As New List(Of DataRow)

    Dim SQL As String = String.Empty
    Dim sqlData As New SQLData

    Private Sub PopulateSuppliers()

        Me.cbSupplier.Items.Clear()
        Me.cbSupplier.DataSource = Nothing

        SetSupplierSQL()
        _results = sqlData.SelectStatement(SQL)

        Me.cbSupplier.DataSource = _results
        Me.cbSupplier.DisplayMember = "id"
        Me.cbSupplier.ValueMember = "name"

    End Sub

Open in new window


Any ideas?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Dirk StraussSenior Full Stack Developer
Distinguished Expert 2017

Commented:
I'm not a VB.NET guy (not anymore), but check that the column name is indeed called name.
Me.cbSupplier.ValueMember = "name"

Open in new window

Have a look at this thread: Setting the ValueMember of a combo box is causing an error.
The column name was Com. ID#, but after renaming the column, it worked:
DataTable dt = rwDB.getCommodities();
dt.Columns["Com. ID#"].ColumnName = "ComID";
cboCommodity.DataSource = dt;
cboCommodity.DisplayMember = "Description";
cboCommodity.ValueMember = "ComID";

Open in new window

NorieAnalyst Assistant

Commented:
Try this.
Me.cbSupplierItems.AddRange(_results.ToArray())

Open in new window

Scott McDaniel (EE MVE )Infotrakker Software
Most Valuable Expert 2012
Top Expert 2014

Commented:
I've also had instances where I had to set the Display and Value members BEFORE setting the Datasource.
Ryan ChongSoftware Team Lead

Commented:
I get an error on "ValueMember"
what's that error message?

on a separate note, I can do that without error by setting Datasource and then DisplayMember and ValueMember.

Author

Commented:
Made the below changes.  No error but still displaying "System.Data.DataRow" in the combobox.
    Private Sub PopulateSuppliers()

        Me.cbSupplier.Items.Clear()
        Me.cbSupplier.DataSource = Nothing

        SetSupplierSQL()
        _results = sqlData.SelectStatement(SQL)

        Me.cbSupplier.Items.AddRange(_results.ToArray())

        Me.cbSupplier.DisplayMember = "name"
        Me.cbSupplier.ValueMember = "id"

    End Sub

Open in new window

Commented:
Below is the solution.  Thanks for your help.
    Private Sub PopulateSuppliers()

        Dim dt As DataTable

        Me.cbSupplier.Items.Clear()
        Me.cbSupplier.DataSource = Nothing

        SetSupplierSQL()
        _results = sqlData.SelectStatement(SQL)

        dt = _results.CopyToDataTable

        Me.cbSupplier.DataSource = dt
        Me.cbSupplier.DisplayMember = "name"
        Me.cbSupplier.ValueMember = "id"

    End Sub

Open in new window

Author

Commented:
Figured out how to resolve the issue and posted the solution.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial