We help IT Professionals succeed at work.

New ways to populate a listbox, vb.net

2,428 Views
Last Modified: 2016-02-18
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

Comment
Watch Question

Ammar GaffarSoftware Engineer
CERTIFIED EXPERT
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
Developer
CERTIFIED EXPERT
Distinguished Expert 2019
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.