Thursday, October 16, 2008

Copy DataRows from one DataTable to Another


"This row already belongs to another table" - Annoying, to say the least!

The workaround, simple but elusive.



public DataTable CopyDataTable(DataTable dtSource, int iRowsNeeded)
{

if (dtSource.Rows.Count > iRowsNeeded)
{
// cloned to get the structure of source
DataTable dtDestination = dtSource.Clone();
for (int i = 0; i < iRowsNeeded; i++)
{
dtDestination.ImportRow(dtSource.Rows[i]);
}
return dtDestination;
}
else
return dtSource;
}



Happy Coding!

Related Posts :



0 comments on "Copy DataRows from one DataTable to Another"

Add your comment. Please don't spam!
Subscribe in a Reader

Post a Comment