2013-08-07 56 views
24

Tôi có danh sách dữ liệu với một số thuộc tính. Tôi muốn chuyển đổi dữ liệu danh sách đó thành bảng dữ liệu. Làm thế nào để chuyển đổi một danh sách thành datable.Cách chuyển danh sách thành bảng dữ liệu

+0

Ông có thể cung cấp cho chúng tôi thông tin về loại của các đối tượng trong danh sách? –

Trả lời

96

Chỉ cần thêm chức năng này và gọi nó, nó sẽ chuyển đổi Danh sách để DataTable.

public static DataTable ToDataTable<T>(List<T> items) 
{ 
     DataTable dataTable = new DataTable(typeof(T).Name); 

     //Get all the properties 
     PropertyInfo[] Props = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance); 
     foreach (PropertyInfo prop in Props) 
     { 
      //Defining type of data column gives proper data table 
      var type = (prop.PropertyType.IsGenericType && prop.PropertyType.GetGenericTypeDefinition() == typeof(Nullable<>) ? Nullable.GetUnderlyingType(prop.PropertyType) : prop.PropertyType); 
      //Setting column names as Property names 
      dataTable.Columns.Add(prop.Name, type); 
     } 
     foreach (T item in items) 
     { 
      var values = new object[Props.Length]; 
      for (int i = 0; i < Props.Length; i++) 
      { 
       //inserting property values to datatable rows 
       values[i] = Props[i].GetValue(item, null); 
      } 
      dataTable.Rows.Add(values); 
     } 
     //put a breakpoint here and check datatable 
     return dataTable; 
} 
+1

cách gọi hàm này.thanks for reply – user2660267

+3

Giả sử bạn có danh sách User.List rm = new List (); Và trong lớp người dùng có các thuộc tính FirstName, LastName, Address etc. Sau đó, để chuyển đổi danh sách này thành datatable, chỉ cần sử dụng DataTable này UserDt = rm.ToDataTable(); –

+1

Chúng ta có cần thêm bất kỳ không gian tên nào để phương thức này hoạt động không? PropertyInfo [] không nhận được. –

7

bạn có thể sử dụng phương thức này và gọi nó như thế này.

DataTable dt = YourList.ToDataTable(); 

public static DataTable ToDataTable<T>(this List<T> iList) 
     { 
      DataTable dataTable = new DataTable(); 
      PropertyDescriptorCollection propertyDescriptorCollection = 
       TypeDescriptor.GetProperties(typeof(T)); 
      for (int i = 0; i < propertyDescriptorCollection.Count; i++) 
      { 
       PropertyDescriptor propertyDescriptor = propertyDescriptorCollection[i]; 
       Type type = propertyDescriptor.PropertyType; 

       if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable<>)) 
        type = Nullable.GetUnderlyingType(type); 


       dataTable.Columns.Add(propertyDescriptor.Name, type); 
      } 
      object[] values = new object[propertyDescriptorCollection.Count]; 
      foreach (T iListItem in iList) 
      { 
       for (int i = 0; i < values.Length; i++) 
       { 
        values[i] = propertyDescriptorCollection[i].GetValue(iListItem); 
       } 
       dataTable.Rows.Add(values); 
      } 
      return dataTable; 
     } 
+0

Chính xác những gì tôi đang tìm kiếm! –

+0

Điều này cho tôi các giá trị số cho một loại 'Danh sách' của' chuỗi'. – Jogi

-1
static DataTable ConvertListToDataTable(List<string[]> list) 
{ 
    // New table. 
    DataTable table = new DataTable(); 
    // Get max columns. 
    int columns = 0; 
    foreach (var array in list) 
    { 
     if (array.Length > columns) 
     { 
      columns = array.Length; 
     } 
    } 
    // Add columns. 
    for (int i = 0; i < columns; i++) 
    { 
     table.Columns.Add(); 
    } 
    // Add rows. 
    foreach (var array in list) 
    { 
     table.Rows.Add(array); 
    } 
    return table; 
} 

protected void Button1_Click(object sender, EventArgs e) 
{ 
    List<string[]> list = new List<string[]>(); 
    list.Add(new string[] { "Column 1", "Column 2", "Column 3" }); 
    list.Add(new string[] { "Row 2", "Row 2" }); 
    list.Add(new string[] { "Row 3" }); 

    // Convert to DataTable. 
    DataTable table = ConvertListToDataTable(list); 
    dataGridView1.DataSource = table; 
} 

Hãy thử điều này

+2

'ConvertListToDataTable (...);' không được biết đến trong dotnet trừ khi bạn truy cập trang này, nơi bạn có thể tìm mã cho phương thức này 'http://social.msdn.microsoft.com/Forums/vstudio/en-US/6ffcb247 -77fb-40b4-bcba-08ba377ab9db/conversion-a-list- – Bellash

Các vấn đề liên quan