List to datatable, list to datatable

Source: Internet
Author: User

List to datatable, list to datatable

Thanks to a friend on the Internet

1 /// <summary> 2 60 // convert the collection class to able 3 61 /// </summary> 4 62 /// <param name = "list"> set </param> 5 63 // <returns> </returns> 6 64 public static DataTable ToDataTable (IList list) 7 65 {8 66 DataTable result = new DataTable (); 9 67 if (list. count> 0) 10 68 {11 69 PropertyInfo [] propertys = list [0]. getType (). getProperties (); 12 70 foreach (PropertyInfo pi in propertys) 13 71 {14 72 result. columns. add (pi. name, pi. propertyType); 15 73} 16 74 17 75 for (int I = 0; I <list. count; I ++) 18 76 {19 77 ArrayList tempList = new ArrayList (); 20 78 foreach (PropertyInfo pi in propertys) 21 79 {22 80 object obj = pi. getValue (list [I], null); 23 81 tempList. add (obj); 24 82} 25 83 object [] array = tempList. toArray (); 26 84 result. loadDataRow (array, true); 27 85} 28 86} 29 87 return result; 30 88} View Code

 

Related Article

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.