Fixed Price Projects          Flexibele IT oplossingen

Software op maat              Web Applications

You have a strong type generic list of any type


  Dim lstPrest As List(Of Object) = ....


Use this function to convert it to a datatable using the properties in the object


        Public Shared Function List2DataTable(dd As Object) As DataTable

                'get columns
                Dim props() As System.Reflection.PropertyInfo = dd(0).GetType().GetProperties()
                Dim table As New DataTable()
                For i As Integer = 0 To props.Count - 1
                    Dim prop As System.Reflection.PropertyInfo = props(i)
                    table.Columns.Add(prop.Name, prop.PropertyType)

                'define values
                Dim values As Object() = New Object(props.Count - 1) {}

                'construct table
                For Each item As Object In dd
                    For i As Integer = 0 To values.Length - 1
                        values(i) = props(i).GetValue(item, Nothing)
                Return table
            Catch ex As Exception
                Throw New Exception("-Exception in " & New System.Diagnostics.StackTrace().GetFrame(0).GetMethod().Name & ":" & ex.Message)
            End Try
        End Function
Categorie: .NET
Actions: E-mail | Permalink |