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

New ways to populate a listbox, vb.net

I have WSCGSoftwareDataSet.xsd with one table. I am looking to see how many solutions are there to populate a listbox with data like: "Select SoftwareID, Title From tblSoftware"

The solutions 1 and 2 are working fine. Can you come up with new ways of doing like solution 3 described below. Or maybe some other solution 4 or solution 5?

Solution 1 (works fine, included for reference):
     '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


Solution 2 (works fine, included for reference):
        Dim results = (From r In WSCGSoftwareDataSet.Tables(0).AsEnumerable() _
                      Select New With { _
                          .SoftwareID = r.Field(Of Integer)("SoftwareID"), _
                          .Title = r.Field(Of String)("Title") _
                      }).ToList()

        ListBox1.DataSource = results

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

Open in new window


Question: Can you come up with Solution 3 described below or possibly with solution 4 or 5 you may have?

Solution 3: How to add a query via code to table adapter or data adapter (whichever makes sense) and use this query ("Select SoftwareID, Title From tblSoftware") to populate the list box. BTW, tblSoftware is the only table in the dataset WSCGSoftwareDataSet.

We can either put the returned result from this  query into a datareader and then add a new datatable (dt1) in dataset called WSCGSoftwareDataSet. And then assig dt1 to listbox datasource or as 4th solution use the datareader to populate the list box.

I gues dataadapter will have no role because noting is communicated with the database. A dataadapter is on play only when there is some activities are taking place between a dataset and a dataset. Here, to populate listbox the interaction doesn't extend beyond the dataset into database.

Your comment on this will be appreciated.

This is yet another solution idea possibly to be developed by you :
Public Class MyData
    Public Property SoftwareID As Integer
    Public Property Title As String
End Class

Open in new window


This is also easy one (it is done):
ListBox1.DataSource = WSCGSoftwareDataSet.Tables(0) 

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

Open in new window

Visual Basic.NET.NET ProgrammingLINQ Query

Avatar of undefined
Last Comment
it_saige

8/22/2022 - Mon
SOLUTION
Ammar Gaffar

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
ASKER CERTIFIED SOLUTION
it_saige

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23