Avatar of Mike Eghtebas
Mike Eghtebas
Flag for United States of America

asked on 

Working with existing dataset vb.net

The dataset in a project (WSCGSoftwareDataSet) has only one table with a few columns. To upload SoftwareID and Title fields from this table into a list box (indexes 0 and 1):
       'create a table object
       Dim dt As New DataTable

       'create two columns
        dt.Columns.Add("SoftwareID", GetType(System.Int32))
        dt.Columns.Add("Title", GetType(System.String))

        ' add select columns from a table in the dataset to dt
        For Each r In WSCGSoftwareDataSet.Tables(0).Rows
            dt.Rows.Add(New Object() {r(0), r(1)})
        Next
       
        ' add dt to listbox datasource
        ListBox1.DataSource = dt

        ' identify display and value members of the listbox
        ListBox1.DisplayMember = "Title"
        ListBox1.ValueMember = "SoftwareID"

Open in new window


The data displays correctly in the list box, but...

Question: Why, upon click on the listbox, the following line doesn't display the value of SoftwareID?

It displays the column caption "SoftwareID" not the corresponding value of it.

    Private Sub ListBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListBox1.SelectedIndexChanged
        MessageBox.Show(ListBox1.ValueMember.ToString())
    End Sub

Open in new window

Visual Basic.NET.NET Programming

Avatar of undefined
Last Comment
Mike Eghtebas

8/22/2022 - Mon