2009-11-08 89 views
0

DataRow [] row = table.Select(「Weight = '57'」); //有1條記錄 DataTable dt = new DataTable();如何將數據行轉換爲數據表格

 foreach (DataRow dr in row) 
       { 
        dt.ImportRow(dr); 
       } 
       dt.AcceptChanges(); 

我的記錄有1條記錄。當我嘗試將datarow []行轉換爲數據表 它運行良好。當我檢查我的dt它不包含任何記錄 。問題是什麼在它

謝謝

回答

2

你應該行復制到新行,而且比它添加到新表。 你不能從一張桌子上拿一排放在另一張桌子上。從另一個站點

代碼示例:

DataTable dtDest = new DataTable(); 
dtDest = dsActivity.Tables[0].Clone(); 
foreach(DataRow dr in dsSrc.Tables[0].Rows) 
{ 
DataRow newRow = dtDest .NewRow(); 
newRow.ItemArray = dr.ItemArray; 
dtDest.Rows.Add(newRow); 
} 

克隆使得dtDest相同類型(列)作爲源。

另一種方式是創建一個新行,添加正確的列並按值複製值。

+0

丹妮你會在代碼中顯示如何做到這一點。 – happysmile

+0

對答案進行了編輯 – Dani

1

嘗試以下操作:

dt.Rows.Add(dr) 
2
DataRow[] rows = table.Select("Weight='57'"); 
DataTable dt = new DataTable(); 

foreach (DataRow item in rows) 
{ 
    // add values into the datatable 
    dt.Rows.Add(item.ItemArray); 
} 
6
DataTable dt = new DataTable(); 

DataRow[] dr = dt.Select("Your string"); 

DataTable dt1 = dr.copyToDataTable(); 
1
DataTable DT = DTMain.Clone(); 
// suppose DT is the table where we need to put datarows and DTMain is a table 
// having the stucture of the datatbale, means you must have some structure for 
// datatable. 

//supposes you have a collection of datarows as 

foreach(DataRow dr in DatarRowColl) 
{ 
    DT.ImportRow(dr); 
} 

DT.AcceptChanges(); 

// here you get the datarows in a datatable. 

快樂編程。

1
dim dt as new DataTable 
dim row as new DataRow 
dim params(10) as string 
''''' imagine we have row and we fill it 
''''' you should create your dataTable as you want 
dt.Columns.Add("column1") 
dt.Columns.Add("column2") 
. 
. 
. 
For Each r As DataRow In row 
    params = {dr.Item("column1"), dr.Item("column2")} 
    dt.Rows.Add(params) 
Next 
dt.AcceptChanges()