Cómo convertir List a dataview en .Net.List <T> to DataView
10
A
Respuesta
18
Mi sugerencia sería convertir la lista en una DataTable, y luego usar la vista predeterminada de la tabla para construir su DataView.
En primer lugar, se debe construir la tabla de datos:
// <T> is the type of data in the list.
// If you have a List<int>, for example, then call this as follows:
// List<int> ListOfInt;
// DataTable ListTable = BuildDataTable<int>(ListOfInt);
public static DataTable BuildDataTable<T>(IList<T> lst)
{
//create DataTable Structure
DataTable tbl = CreateTable<T>();
Type entType = typeof(T);
PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(entType);
//get the list item and add into the list
foreach (T item in lst)
{
DataRow row = tbl.NewRow();
foreach (PropertyDescriptor prop in properties)
{
row[prop.Name] = prop.GetValue(item);
}
tbl.Rows.Add(row);
}
return tbl;
}
private static DataTable CreateTable<T>()
{
//T –> ClassName
Type entType = typeof(T);
//set the datatable name as class name
DataTable tbl = new DataTable(entType.Name);
//get the property list
PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(entType);
foreach (PropertyDescriptor prop in properties)
{
//add property as column
tbl.Columns.Add(prop.Name, prop.PropertyType);
}
return tbl;
}
A continuación, obtener vista predeterminada de la DataTable:
DataView NewView = MyDataTable.DefaultView;
Un ejemplo completo sería la siguiente:
List<int> ListOfInt = new List<int>();
// populate list
DataTable ListAsDataTable = BuildDataTable<int>(ListOfInt);
DataView ListAsDataView = ListAsDataTable.DefaultView;
+1
Una corrección menor CreateTable también debería ser estática. – user3141326
Cuestiones relacionadas
- 1. DataTable to List <object>
- 2. Sharepoint List to PDF report
- 3. Diccionario <StudentType, List <Student>> to IDictionary <StudentType, IList <Student>>?
- 4. ObservableCollection <> vs. List <>
- 5. jQuery checkbox values to comma separated list
- 6. std :: vector to boost :: python :: list
- 7. List <> propia comparador
- 8. Casting List <> de la clase derivada a List <> de la clase base
- 9. Lista <Map <String, Object >> to org.json.JSONObject?
- 10. Cómo filtrar datos en dataview
- 11. Obteniendo el valor de std :: list <> :: iterator to pointer?
- 12. convert & lt to <xml document
- 13. IEnumerable <> to IList <>
- 14. C# Casting a List <ObjBase> como List <Obj>
- 15. Lista <? extiende Base> VS List <Base>
- 16. Queue <T> vs List <T>
- 17. C# List <> GroupBy 2 Valora
- 18. ¿Está std :: list <> :: sort stable?
- 19. Android save List <String>
- 20. return unknown Generic List <T>
- 21. C# List <> Add() método rendimiento
- 22. cómo instanceof List <MyType>?
- 23. Convertir diccionario a List <KeyValuePair>
- 24. jsRender loop a List <string>
- 25. Sort List <DateTime> Descending
- 26. SlickGrid RemoteModel vs. Dataview Modelo
- 27. add-to-list 'load-path no parece funcionar
- 28. DataView Ordenado tabla de datos a
- 29. ¿Por qué List <>. OrderBy LINQ es más rápido que IComparable + List <>. ¿Ordenar en modo Debug?
- 30. Downcasting shared_ptr <Base> to shared_ptr <Derived>?
Un más orientado a objetos que la respuesta aceptada sería usar un método similar a las respuestas a esta pregunta. [Ordene una lista usando expresiones de consulta] (http://stackoverflow.com/questions/695906/sort-a-listt-using-query-expressions) Esto supone que la única razón por la que desea que aparezca una lista una vista de datos es para la funcionalidad de clasificación. –
Amicable