How to populate an array from a DataSet in VB.NET

13,064

Solution 1

Update:

Assuming you want an array of String:

Dim arr As String() = (From myRow In ds.Tables(0).AsEnumerable
                       Select myRow.Field(Of String)("yourColumnName")).ToArray

or a list:

Dim list As List(Of String) = (From myRow In ds.Tables(0).AsEnumerable
                               Select myRow.Field(Of String)("yourColumnName")).ToList

Old:

Make sure the DisplayMember is set to the name of the column you want to see:

comboBox1.DataSource = ds.Tables(0)
comboBox1.DisplayMember= "NameOfColumn"

You might also want to set the ValueMember property to the ID field name from your dataset.

Solution 2

Dim objDataSet As New DataSet

objDataSet = DataSetConsultas("SELECT Nombres, IDTarjeta from Alumnos")

Dim arr As String() = (From myRow In objDataSet.Tables(0).AsEnumerable
                    Select myRow.Field(Of String)("Nombres")).ToArray

cboAlumnos.Items.Clear()
cboAlumnos.Items.AddRange(arr)

Where Nombres, IDTarjeta are rows in the DB, and Alumnos is the name of the table

Share:
13,064
Naad Dyr
Author by

Naad Dyr

Updated on June 04, 2022

Comments

  • Naad Dyr
    Naad Dyr almost 2 years

    I'm trying to populate an array from a dataset with only a specific column using VB.NET 2010. Is there any code to populate the array directly or must I make use of a query?