2011-08-19 46 views
0

我的問題arised在這裏的接受答案:的DataRow []聚合函數C# - 附錄

DataRow[] Aggregate Functions C#

在這個問題的答案,怎麼會是 「行」 對象intialized?

編輯: 在我的情況下,我有mydataset.Tables [0] .Rows集合,從中我需要SUM特定的列。

+1

你的出發點是Linq的問題,但你以後的編輯讓我懷疑,如果'DataTable.Compute '不會只是你需要的。 –

+0

這可能只是工作。我會嘗試發佈更新。 – FMFF

回答

1

大概從LINQ查詢,如:

List<DataRow> rows = (from row in mydataset.Tables[0].AsEnumerable() 
         select row).ToList(); 
+0

我試過了,並得到'不能隱式轉換類型'System.Data.EnumerableRowCollection '到'System.Collections.Generic.List ' – FMFF

+1

@FMFF抱歉,退房我的更新 –

2

那麼你可以創建自己的DataRow

List<DataRow> listOfRows = new List<DataRow>(); 

列表,或者你可以從GridView控件得到他們的集合。

var rows = gridView.Tables[0].Rows; //will return a collection of DataRows from your Gridview 

或者從DataSet中

var rows = dataSet.Tables[0].Rows; 
+0

當我嘗試做這樣 '名單行= myDataSet.Tables [0] .Rows;' 我得到這個: 無法隱式轉換類型 'System.Data.DataRowCollection' 到「System.Collections中.Generic.List ' – FMFF

+0

正確,因爲您將返回與列表不同的DataRows集合。嘗試這個。 var rows = myDataSet.Tables [0] .Rows; – Jethro

+0

@Jethro Sum方法對於DataRowCollection不存在,所以這不是很有幫助 –