2013-08-07 75 views
24

我有一些財產的數據列表。我想將該列表數據轉換爲數據表。如何將列表轉換爲datable。如何列表轉換成數據表

+0

你能不能給我們介紹一下類型列表中的對象的信息? –

回答

96

只需添加此功能,並調用它,它就會列表轉換爲數據表

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

如何調用該function.thanks答覆 – user2660267

+3

假設你有User.List RM =新名單的名單();並且在User類中有FirstName,LastName,Address等屬性。然後將這個列表轉換爲數據表,只需使用這個DataTable UserDt = rm.ToDataTable(); –

+1

我們是否需要爲此方法添加任何名稱空間才能工作? PropertyInfo []沒有被識別。 –

7

你可以使用這個擴展方法,並調用它。

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

正是我在找的東西! –

+0

這給了我'string'類型'List'的數值。 – 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; 
} 

試試這個

+2

'ConvertListToDataTable(...);'在dotnet的,除非你訪問這個頁面,你可以爲這個方法找到的代碼是不知道'http://social.msdn.microsoft.com/Forums/vstudio/en-US/6ffcb247 -77fb-40B4-BCBA-08ba377ab9db /轉換-A-列表 - – Bellash