Come convertire List in un dataview in .Net.Elenco <T> in DataView
10
A
risposta
18
Il mio suggerimento sarebbe quello di convertire l'elenco in un DataTable e quindi utilizzare la vista predefinita della tabella per creare il tuo DataView.
In primo luogo, è necessario costruire la tabella di dati:
// <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;
}
Avanti, ottenere visualizzazione predefinita del DataTable:
DataView NewView = MyDataTable.DefaultView;
Un esempio completo potrebbe essere il seguente:
List<int> ListOfInt = new List<int>();
// populate list
DataTable ListAsDataTable = BuildDataTable<int>(ListOfInt);
DataView ListAsDataView = ListAsDataTable.DefaultView;
+1
Anche una correzione minore CreateTable dovrebbe essere statica. – user3141326
Problemi correlati
- 1. Come utilizzare l'espressione del generatore CMake $ <TARGET_FILE: tgt>?
- 2. Converti elenco <DerivedClass> in elenco <BaseClass>
- 3. Filtraggio della riga DataView
- 4. dataview Ordinato per DataTable
- 5. Elenco ed elenco <?> in Java
- 6. Looping di righe in un DataView
- 7. Come filtrare i dati in dataview
- 8. Come copiare un elenco <> in un altro elenco <> con Comparsion in C#
- 9. Inizializza lista <Elenco <Integer>> in Java
- 10. Elenco <custom> in Excel C#
- 11. Elenco <int> in C#
- 12. Converti dizionario in elenco <KeyValuePair>
- 13. Sencha Touch: Componente su DataView che assomiglia ad Elenco intestazione gruppo
- 14. Convertire 'ArrayList' in 'Elenco <string>' (o 'Elenco <T>') utilizzando LINQ
- 15. Converti SqlDataSource per DataTable e DataView
- 16. Elenco <? estende MyType>
- 17. Converti Lista <T> in un altro elenco <T> che contiene un altro elenco <T>
- 18. SlickGrid funzione di filtro DataView comportarsi in modo anomalo
- 19. ritorno Queryable <T> o Elenco <T> in un repository <T>
- 20. Casting IEnumerable <T> a Elenco <T>
- 21. Elenco di espressioni <Func <T, TProperty >>
- 22. Casting Elenco popolato <BaseClass> a elenco <ChildClass>
- 23. Usa LINQ per ottenere elementi in un elenco <>, che non sono in un altro elenco <>
- 24. utilizzando LINQ in C# per convertire un elenco <string> in un elenco <char>
- 25. Elenco <stringhe separate da virgole> => Elenco <string>?
- 26. Elenco prestazioni SqlDataReader <string[]> o Elenco <object[]>
- 27. Elenco <object> Per elenco <T> utilizzando la riflessione
- 28. Elenco <int> a IEnumerable <IComparable>
- 29. Elenco <? estende Base> Lista VS <Base>
- 30. Elenco <IJob> .AddRange (Elenco <Job>) Non funziona
A un modo più orientato agli oggetti rispetto alla risposta accettata sarebbe utilizzare un metodo simile alle risposte a questa domanda. [Ordina un elenco utilizzando le espressioni di query] (http://stackoverflow.com/questions/695906/sort-a-listt-using-query-expressions) Questo presume che l'unica ragione per cui desideri sia un elenco un dataview è per la funzionalità di ordinamento. –
Amicable