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
VB.NET select columns from dataset
提问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 DataRow
extension method Field
which 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 DataTable
with a subset of columns of the original DataTable you can use the DataView
of the table and it's ToTable
method:
编辑:如果您想要另一个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")