2012-01-31 41 views
2

我有2個數據集,如下圖所示,我需要這些數據集合併成1:如何排序和順序合併數據集

數據集1

date    reason  total 
12 aug 2010  inactive 123 
19 aug 2010  inactive 45 
20 sep 2010  inactive 145 
02 nov 2010  inactive 95 
25 dec 2010  inactive 44 

dataset2

date    reason  total 
12 aug 2010  active 12 
21 aug 2010  active 45 
20 sep 2010  active 45 
02 nov 2010  active 45 
26 dec 2010  active 45 

我可以合併通過使用Merge方法的數據集,但我怎麼對數據集進行排序,得到的結果是這樣的:

date    reason  total 
12 aug 2010  inactive  123 
12 aug 2010  active  12 
19 aug 2010  inactive  45 
21 aug 2010  active  45 
20 sep 2010  inactive 145 
20 sep 2010  active  45 
02 nov 2010  inactive 95 
02 nov 2010  active  45 
25 dec 2010  inactive 44 
26 dec 2010  active  45 
+0

什麼是日期列的數據類型?文字或日期?如果是日期請參閱http://stackoverflow.com/questions/513961/c-sharp-how-do-i-sort-a-datatable-by-date – 2012-01-31 06:24:03

回答

4

假設你想在升序日期順序及原因從大到小的順序,你可以使用一個DataView的Sort屬性(DataView.Sort Property):

// Assuming the merged table is the first and only table in the DataSet. 
DataView dv = new DataView(dataSet1.Tables[0]); 

dv.Sort = "date, reason DESC"; 

我沒有測試過這一點 - 就在我的頭頂。應該指出你在正確的方向。

+0

工作良好蒂姆謝謝 – happysmile 2012-02-02 02:20:10