2

我有以下内容:

    Dim dt As DataTable = ds.Tables(0)

Tables(0) 有大约 20 列。我喜欢只选择一对。“PrID”是字段之一。

我试过

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

没有任何成功。任何的想法?

4

3 回答 3

4

一种方法是使用强类型并支持可空类型的DataRow扩展方法:Field

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

编辑:如果您想要另一个DataTable具有原始 DataTable 的列子集的另一个,您可以使用DataView该表及其ToTable方法:

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")
于 2012-08-23T21:41:54.663 回答
0

您可以使用 PRID 列。

    Dim dt As New DataTable
    Dim columns As String() = "PrID".Split(",")
    dt = ds.Tables(0).DefaultView.ToTable(String.Empty, False, columns)
于 2012-08-24T03:58:23.320 回答
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")
于 2016-09-07T06:55:51.840 回答