2

I have the following:

    Dim dt As DataTable = ds.Tables(0)

Tables(0) has about 20 columns. I like to select only a couple. "PrID" is one of the fields.

I tried

    Dim dt As DataTable = ds.Tables(0).Select("PrID")

without any success. Any idea?

3 Answers 3

5

One way is using the DataRow extension method Field which is strongly typed and supports nullable types:

For Each row As DataRow in ds.Tables(0).Rows
    Dim PrID As Int32 = row.Field(Of Int32)("PrID")
Next

Edit: If you want another DataTable with a subset of columns of the original DataTable you can use the DataView of the table and it's ToTable method:

Dim displayView = New DataView(ds.Tables(0))
' if you're only interested in: PrID, Col2, Col3
Dim subset As DataTable = displayView.ToTable(false, "PrID", "Col2", "Col3")
Sign up to request clarification or add additional context in comments.

Comments

0

You can get PRID column using.

    Dim dt As New DataTable
    Dim columns As String() = "PrID".Split(",")
    dt = ds.Tables(0).DefaultView.ToTable(String.Empty, False, columns)

Comments

0
    'first create a new Dataview 
    Dim [Dataview] As New DataView

    'here add the table to Dataview you want to filter its columns
    [Dataview].Table = Ds.Tables(" here Write TableName ")

    'here you can display selected Columns in Datatable 
    Dim [datatable] As DataTable = [Dataview].ToTable(False, "desired column Name ", "desired Column Name")
    'here you can display selected Columns  in DatagridView1
    DataGridView1.DataSource = [Dataview].ToTable(False, "desired column Name ", "desired Column Name")

1 Comment

if you want to display selected Columns in DatagridView1 then there is no need to first get Data in Datatable and then set it to DataGridView1. Simply get selected columns and set to DataGridview1.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.