Link to home
Start Free TrialLog in
Avatar of Mike Eghtebas
Mike EghtebasFlag 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

SOLUTION
Avatar of Ammar Gaffar
Ammar Gaffar
Flag of United Arab Emirates 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
ASKER CERTIFIED SOLUTION
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