我有以下内容:
Dim dt As DataTable = ds.Tables(0)
Tables(0) 有大约 20 列。我喜欢只选择一对。“PrID”是字段之一。
我试过
Dim dt As DataTable = ds.Tables(0).Select("PrID")
没有任何成功。任何的想法?
我有以下内容:
Dim dt As DataTable = ds.Tables(0)
Tables(0) 有大约 20 列。我喜欢只选择一对。“PrID”是字段之一。
我试过
Dim dt As DataTable = ds.Tables(0).Select("PrID")
没有任何成功。任何的想法?
一种方法是使用强类型并支持可空类型的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")
您可以使用 PRID 列。
Dim dt As New DataTable
Dim columns As String() = "PrID".Split(",")
dt = ds.Tables(0).DefaultView.ToTable(String.Empty, False, columns)
'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")