lundi 2 mars 2015

How to copy specific rows from DataTable to another one

I want to copy some rows of data table, to another one. I tried this code :



DataTable Result = new DataTable();

for(int i = 5; i < PageSize && i < TransactionDataTable.Rows.Count ; i++)
{
DataRow dr = (DataRow)TransactionDataTable.Rows[i];

Result.ImportRow(dr);
}

string MobileNumber = TransactionDataTable.Rows[0]["MobileRegistration_MobileNumber"].ToString();
string MobileNumber2 = Result.Rows[0]["MobileRegistration_MobileNumber"].ToString();

TransactionDataTable is a dataTable with more than 1000 rows.


in above code, MobileNumber has proper vallue, but MobileNumber2 dosent have. I got this error in last line ( in assigning MobileNumber2 value) :



Additional information: Column 'MobileRegistration_MobileNumber' does not belong to table .


It seems that the rows didnt copy properly, in Result dataTable.


whats the wrong with this code?


and I tried Result.Rows.Add(dr); instead of Result.ImportRow(dr); but an exception throw with this information:



This row already belongs to another table.


Thanks for any helping...


Aucun commentaire:

Enregistrer un commentaire