DataTable Data Paging

Source: Internet
Author: User

Using System;
Using System.Collections.Generic;
Using System.Data;
Using System.Linq;
Using System.Reflection;
Using System.Text;
Using System.Threading.Tasks;

Namespace Data Paging
{
public class Student
{
public int? Stuno {get; set;}
public string Name {get; set;}
public string Sex {get; set;}
public int Age {get; set;}
public override string ToString ()
{
return this. Stuno + ":" + this. Name + ":" + this. Sex + ":" + this. Age;
}

}

public static Class Datatableandlistexetension
{
public static list<t> tolist<t> (this DataTable table) where T:new ()
{
list<t> list = new list<t> ();
Type T = typeof (T);
Propertyinfo[] PS = t.getproperties ();
foreach (DataRow dr in table. Rows)
{
T obj = new T ();
foreach (DataColumn col in table. Columns)
{

foreach (PropertyInfo p in PS)
{
if (p.name = = Col. ColumnName)
{
if (!p.propertytype.isgenerictype)
{
P.setvalue (obj, String. IsNullOrEmpty (Dr[col. ColumnName]. ToString ())? Null:Convert.ChangeType (Dr[col. ColumnName], p.propertytype));

}
Else
{
Type generictypedefinition = P.propertytype.getgenerictypedefinition ();
if (generictypedefinition = = typeof (Nullable<>))
{
P.setvalue (obj, String. IsNullOrEmpty (Dr[col. ColumnName]. ToString ())? Null:Convert.ChangeType (Dr[col. ColumnName], Nullable.getunderlyingtype (P.propertytype));
}

}

}

}

}
List. ADD (obj);
}
return list;
return null;

}
public static DataTable todatatable<t> (this list<t> List)
{
Type T = typeof (T);
Propertyinfo[] PS = t.getproperties ();
DataTable dt = new DataTable ();
foreach (PropertyInfo p in PS)
{
Type type = P.propertytype;
if (!p.propertytype.isgenerictype)
{
Dt. Columns.Add (p.name, type);

}
Else
{
Type generictypedefinition = P.propertytype.getgenerictypedefinition ();
if (generictypedefinition = = typeof (Nullable<>))
Dt. Columns.Add (P.name, Nullable.getunderlyingtype (P.propertytype));
}


}
foreach (T stu in list)
{

DataRow dr = dt. NewRow ();
foreach (PropertyInfo p in PS)
{
Dr[p.name] = P.getvalue (stu);

}
Dt. Rows.Add (DR);
}
return DT;
return null;
}

}
public class Program
{
public static list<student> stulist = new list<student> ();
static void Main (string[] args)
{
Stulist.add (New Student () {Stuno = 1, Name = "1", Sex = "1", age = 1});
Stulist.add (New Student () {Stuno = 2, Name = "2", Sex = "2", age = 2});
Stulist.add (New Student () {Stuno = 3, Name = "3", Sex = "3", age = 3});
Stulist.add (New Student () {Stuno = 4, Name = "4", Sex = "4", age = 4});
Stulist.add (New Student () {Stuno = 5, Name = "5", Sex = "5", age = 5});

DataTable dt = stulist.todatatable<student> ();
//
DataTable newtable = dt. Clone ();
StringBuilder sb = new StringBuilder ();
int rowCount = dt. Rows.Count;
int pageSize = 2;
int pages = 0;
pages = Rowcount/pagesize;
Console.WriteLine ("Start of page content");
if (RowCount <= pageSize)
{
for (int j = 1; J <= RowCount; j + +)
{

DataRow NEWDR = Newtable.newrow ();
var olddr = dt. ROWS[J-1];
Newdr.itemarray = olddr.itemarray;//old table structure row to new table structure row
Newtable.importrow (OLDDR);

}

Newtable.tolist<student> (). ForEach (A = = {Console.WriteLine (a.tostring ());});
}
Else
{
for (int pageIndex = 1; pageIndex <= pages; pageindex++)
{
for (int j = (pageIndex-1) * pageSize + 1; J <= PageIndex * pageSize; j + +)
{
DataRow NEWDR = Newtable.newrow ();
var olddr = dt. ROWS[J-1];
Newdr.itemarray = olddr.itemarray;//old table structure row to new table structure row
Newtable.importrow (OLDDR);
}

Newtable.tolist<student> (). ForEach (A = = {Console.WriteLine (a.tostring ());});

}
if (rowCount% pageSize! = 0)
{
int rows = rowCount% PageSize;
for (int j = pages * pageSize + 1; J <= pages * pageSize + rows; j + +)
{
DataRow NEWDR = Newtable.newrow ();
var olddr = dt. ROWS[J-1];
Newdr.itemarray = olddr.itemarray;//old table structure row to new table structure row
Newtable.importrow (OLDDR);
}
Newtable.tolist<student> (). ForEach (A = = {Console.WriteLine (a.tostring ());});
}
}
Console.WriteLine ("End of page content");
//
DataTable DT2 = dt. Clone ();
list<student> stus = dt. Tolist<student> ();
Console.WriteLine ("DT". Tolist<student> () Output student list ");
Stus. ForEach (A = = {Console.WriteLine (a.tostring ());});
Console.ReadLine ();
foreach (DataRow odr in dt.) Rows)
{
DataRow NDR = DT2. NewRow ();
Ndr. ItemArray = ODR. ItemArray;
DT2. ImportRow (ODR);
}
}
}
}

DataTable Data Paging

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.