2010-10-06 50 views
2

idLocals是數據庫主鍵異常,同時將多行添加到數據表。無法保存<System.Data.DataRowCollection>

填充DataTable中

oDtLocalCharge = bll.GetData(); 

我正在複製的數據表中的另一個功能

dtLocalCharges = oDtLocalCharge.Copy(); 

並嘗試使用以下代碼添加行

DataRowCollection drr = oDtLocalCharge.Rows; 
dtLocalCharges.Rows.Add(drr); 

當我嘗試將行添加到數據表中,我得到如下錯誤


Error: 
{System.ArgumentException: Unable to cast object of type 'System.Data.DataRowCollection' to type 'System.IConvertible'.Couldn't store <System.Data.DataRowCollection> in idLocals Column. Expected type is Int32. ---> System.InvalidCastException: Unable to cast object of type 'System.Data.DataRowCollection' to type 'System.IConvertible'. 

可能是主鍵idLocals導致問題 是什麼問題?

我該如何解決這個問題?我想在dtLocalCharges表中添加多個行

回答

4

首先,您不能使用Add()和DataRowCollection。您需要分別添加每一行。您可能需要使用NewRow,複製值然後調用Add。

請先嚐試:

DataRowCollection drr = oDtLocalCharge.Rows; 
foreach (var row in drr) dtLocalCharges.Rows.Add(row); 
+1

datatable.ImportRow(OtherDataTable)! – Thakur 2010-10-07 06:35:27

+0

好的!我沒有看整個DataTable API。 – 2010-10-07 11:49:00

1

這是導入一個數據表中的所有行到另一個數據表的最佳方式:所做的工作對我來說

datatable1.Load(New DataTableReader(datatable2)) 
+0

這個選項最適合我。 – kitndirangu 2014-09-15 11:30:38

相關問題