Convert Array of Objects to Data Table
Normally in a web application, the Presentation Layer retrieves an Array/Collection of business objects as the data source to bind to GridView control. However sometimes we do need a DataTable to return. In such cases we need convert an Array/Collection of object to a DataTable.
Now let's look into te code.
[C#]
public DataTable ConvertArrayToTable(Array myList)
{
DataTable dt = new DataTable();
if (myList.Length > 0)
{
PropertyInfo[] propInfos = myList.GetValue(0).GetType().GetProperties();
foreach (PropertyInfo propInfo in propInfos)
{
dt.Columns.Add(propInfo.Name, propInfo.PropertyType);
}
foreach (object tempObject in myList)
{
DataRow dr = dt.NewRow();
for (int i = 0; i < propInfos.Length; i++)
{
dr[i] = propInfos[i].GetValue(tempObject, null);
}
dt.Rows.Add(dr);
}
}
return dt;
}
[VB.NET]
Public Shared Function ConvertArrayToDataTable(ByVal list As ArrayList) As DataTable
Dim table As New DataTable()
If list.Count > 0 Then
Dim iterator As IEnumerator = list.GetEnumerator()
iterator.MoveNext()
Dim propInfos As PropertyInfo() = iterator.Current.GetType().GetProperties()
For Each propInfo As PropertyInfo In propInfos
table.Columns.Add(propInfo.Name, propInfo.PropertyType)
Next
For Each obj As Object In list
Dim row As DataRow = table.NewRow()
For i As Int32 = 0 To propInfos.Count - 1
row.Item(i) = propInfos(i).GetValue(obj, Nothing)
Next
table.Rows.Add(row)
Next
End If
Return table
End Function
References
[@more@]來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/13651903/viewspace-1043351/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- PDO--PHP Data ObjectsPHPObject
- 2.1.3.3 Container Data Objects in a CDBAIObject
- C# convert ImageSource to byte arrayC#
- JavaScript 特殊物件 Array-Like Objects 詳解JavaScript物件Object
- Convert Range-Partitioned Table To Interval-Range-Partitioned Table
- rdo(remote data objects) repo openstack icehouseREMObject
- Leetcode-Convert Sorted Array to BSTLeetCode
- Introduce to Data Storage Objects in ASP.NETObjectASP.NET
- 「Python」Convert map object to numpy array in python 3PythonObject
- array2json() - Convert PHP arrays to JSONJSONPHP
- Leetcode Convert Sorted Array to Binary Search TreeLeetCode
- Spring Data MongoDB cascade save on DBRef objectsSpringMongoDBObject
- the procedure:delete the data of one tabledelete
- [leetcode]convert-sorted-array-to-binary-search-treeLeetCode
- Convert Sorted Array to Binary Search Tree leetcode javaLeetCodeJava
- 2.3.2.3 Data-Linked Application Common ObjectsAPPObject
- object_id and data_object_id in dba_objectsObject
- Get detailed table(many other objects) structure with dbms_metadataAIObjectStruct
- js類陣列物件(array-like objects)簡單介紹JS陣列物件Object
- RCNN中函式配置-convert_data_to_tfrecordCNN函式
- How to monitor data transaction on one table
- Import Data From MS Excel to DataSet without using COM ObjectsImportExcelObject
- LUA基礎: TABLE, ARRAY, NAMESPACE, LUA的物件導向namespace物件
- PHP設計模式-DAO (Data Access Objects) 資料訪問物件模式PHP設計模式Object物件
- [UE] Data Table 對比工具 —— 用於 Data Table 對比以前的資料,檢視有什麼修改
- use azure data studio to create external table for oracleOracle
- ObjectsObject
- 解決Tensorflow ValueError: Failed to convert a NumPy array to a Tensor (Unsupported object type numpy.ndarray)ErrorAIObject
- SQL Server 表分割槽(partitioned table/Data Partitioning)SQLServer
- Data Warehouse Guide閱讀筆記(七):partition tableGUIIDE筆記
- LeetCode解題報告 108. Convert Sorted Array to Binary Search Tree [medium]LeetCode
- 【檢視】oracle 資料字典檢視之 DBA_OBJECTS / ALL_OBJECTS / USER_OBJECTS(OBJ)OracleObject
- Oracle dba_objects和all_objects 最大的區別OracleObject
- ABP框架系列之五十二:(Validating-Data-Transfer-Objects-驗證資料傳輸物件)框架Object物件
- 淺談Associated ObjectsObject
- Destroying Window ObjectsObject
- create objects inmemory optionsObject
- Naming Database ObjectsDatabaseObject