Add range rows from one DataTable to another

Viewed 12993

Have a DataTable and I need to create another one having a subset range of rows from the first. So I want the second DataTable to have row n till n + y from the first one. How can I do this please?

DataTable limitData = new DataTable();
for (int rowIndex = startingRow; rowIndex < endingRow; rowIndex++)
{
    limitData.Rows.Add(columnarData.Rows[rowIndex].ItemArray);
}

gives an error: "Input array is longer than the number of columns in this table."

The code is in C# .NET 4.0

3 Answers

Union() also work:

dt1 = dt1.AsEnumerable().Union(dt2.AsEnumerable()).CopyToDataTable();

Reference from this post.

Related