I want to Split Datatable rows based on number

I have datatable and i want to split the number of rows based on foreach 12 row for example if the number of rows is 61 : that's mean 61 / 12 = 6 Tables 5 tables with 12 rows and the table number 6 have 3 rows C# Thanks alot

Answers


You could use following LINQ query:

DataTable[] splittedtables = tbl.AsEnumerable()
    .Select((row, index) => new { row, index })
    .GroupBy(x => x.index / 12)  // integer division, the fractional part is truncated
    .Select(g => g.Select(x => x.row).CopyToDataTable())
    .ToArray();

The array contains 6 tables, 5 with 12 rows, the last one has the remaining row.

Checked with this sample-data:

DataTable  tbl = new DataTable();
tbl.Columns.Add("Column");
for(int i=0; i < 61; i++)
    tbl.Rows.Add(i.ToString()); 

You need to add using System.Linq;.


Need Your Help

generate dependencies for a makefile for a project in C/C++

c++ c makefile dependencies

I have a project that has a makefile with broken dependencies. Is there any best known way to generate a list of dependencies for the project that I can use in the makefile, other than examining e...

How to create a horizontal list of pictures with title in android?

java android image listview module

I want to have a horizontal scrolling list that include some pictures with their titles.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.