Naad Dyr
Naad Dyr

Reputation: 137

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

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?

Upvotes: 2

Views: 13995

Answers (2)

alundy
alundy

Reputation: 857

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.

Upvotes: 4

Al Mendieta
Al Mendieta

Reputation: 11

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

Upvotes: 1

Related Questions