VB.NET 从数据集中选择列

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12100360/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 16:15:35  来源:igfitidea点击:

VB.NET select columns from dataset

vb.net

提问by Nate Pet

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.

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

I tried

我试过

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

without any success. Any idea?

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

回答by Tim Schmelter

One way is using the DataRowextension method Fieldwhich is strongly typed and supports nullable types:

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

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 DataTablewith a subset of columns of the original DataTable you can use the DataViewof the table and it's ToTablemethod:

编辑:如果您想要另一个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")

回答by Mohmedsadiq

You can get PRID column using.

您可以使用获取 PRID 列。

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

回答by Farooq Ahmed

    '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")