2012-01-06 45 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