2012-01-06 71 views
1

我有一个DataTableVB.NET查询数据表

|------------| 
| id | x | y | 
|------------| 
| 1 | 1 | 1 | 
| 2 | 1 | 2 | 
| 3 | 2 | 1 | 
| 4 | 2 | 2 | 
|------------| 

我想通过x的值来过滤这个数据表来与人口数据集和数据表的一个新的DataTable

if x = 1 
|------------| 
| id | x | y | 
|------------| 
| 1 | 1 | 1 | 
| 2 | 1 | 2 | 
|------------| 

or x = 2 
|------------| 
| id | x | y | 
|------------| 
| 3 | 2 | 1 | 
| 4 | 2 | 2 | 
|------------| 

查询仍然困扰着我。谢谢你的帮助。

+0

查收我的回答你刚才的问题http://stackoverflow.com/questions/8749526/vb- net-databinding从2表 – 2012-01-06 00:52:12

+0

亲切地检查我的答案您的上一个问题http://stackoverflow.com/questions/8749526/vb-net-databinding-from-2-tables – 2012-01-06 00:52:40

回答

1

您可以尝试创建新的DataTable并克隆原始的DataTable以引入架构和约束。然后筛选行并将其添加到新的DataTable中。

Dim newDT As DataTable = oldDT.Clone() 

Dim filter As string = "x = 1"; 

//get the rows from the that have been filtered 
DataRow[] filteredRows = oldDT.Select(filter); 

//add the rows to the new datatable 
For Each dr As DataRow In filteredRows 
    newDT.ImportRow(dr) 
Next