How to convert List to Datatable in vb.net
Posted
by
Samir R. Bhogayta
on Samir ASP.NET with C# Technology
See other posts from Samir ASP.NET with C# Technology
or by Samir R. Bhogayta
Published on 2014-06-02T12:32:00.004+05:30
Indexed on
2014/06/02
15:53 UTC
Read the original article
Hit count: 204
Filed under:
Public Function ConvertToDataTable(Of T)(ByVal list As IList(Of T)) As DataTable
Dim table As New DataTable()
Dim fields() As FieldInfo = GetType(T).GetFields()
For Each field As FieldInfo In fields
table.Columns.Add(field.Name, field.FieldType)
Next
For Each item As T In list
Dim row As DataRow = table.NewRow()
For Each field As FieldInfo In fields
row(field.Name) = field.GetValue(item)
Next
table.Rows.Add(row)
Next
Return table
End Function
Dim table As New DataTable()
Dim fields() As FieldInfo = GetType(T).GetFields()
For Each field As FieldInfo In fields
table.Columns.Add(field.Name, field.FieldType)
Next
For Each item As T In list
Dim row As DataRow = table.NewRow()
For Each field As FieldInfo In fields
row(field.Name) = field.GetValue(item)
Next
table.Rows.Add(row)
Next
Return table
End Function
© Samir ASP.NET with C# Technology or respective owner