top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

How to convert Generic List to DataTable in ASP.NET?

+1 vote
321 views
How to convert Generic List to DataTable in ASP.NET?
posted Feb 5, 2016 by Latha

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

+1 vote
 
Best answer

C#

using System.Data;
using System.Reflection;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        List<State> States = new List<State>();

        States.Add(new State() { Name = "Texas", Abbr = "TX", Capital = "Austin" });
        States.Add(new State() { Name = "Georgia", Abbr = "GA", Capital = "Atlanta" });
        States.Add(new State() { Name = "New York", Abbr = "NY", Capital = "Albany" });

        DataTable table = ListToDataTable(States);
        gvStates.DataSource = table;
        gvStates.DataBind();
    }

    public DataTable ListToDataTable(List items)
    {

        DataTable dataTable = new DataTable(typeof(T).Name);
        PropertyInfo[] Properties = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance);

        foreach (PropertyInfo propInfo in Properties)
        {
            dataTable.Columns.Add(propInfo.Name);
        }

        foreach (T item in items)
        {
            var values = new object[Properties.Length];

            for (int i = 0; i < Properties.Length; i++)
            {
                values[i] = Properties[i].GetValue(item, null);
            }
            dataTable.Rows.Add(values);
        }
        return dataTable;
    }
}

public class State
{
    private string _Name = "";
    private string _Abbr = "";
    private string _Capital = "";

    public string Name
    {
        set
        {
            this._Name = value;
        }
        get
        {
            return this._Name;
        }
    }

    public string Abbr
    {
        set
        {
            this._Abbr = value;
        }
        get
        {
            return this._Abbr;
        }
    }
    public string Capital
    {
        set
        {
            this._Capital = value;
        }
        get
        {
            return this._Capital;
        }
    }
}

VB.NET

Imports System.Data
Imports System.Reflection

Partial Class Default2
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim States As New List(Of State)()

        States.Add(New State() With { _
         .Name = "Texas", _
         .Abbr = "TX", _
         .Capital = "Austin" _
        })
        States.Add(New State() With { _
         .Name = "Georgia", _
         .Abbr = "GA", _
         .Capital = "Atlanta" _
        })
        States.Add(New State() With { _
         .Name = "New York", _
         .Abbr = "NY", _
         .Capital = "Albany" _
        })

        Dim table As DataTable = ListToDataTable(States)
        gvStates.DataSource = table
        gvStates.DataBind()
    End Sub

    Public Function ListToDataTable(Of T)(ByVal items As List(Of T)) As DataTable

        Dim dataTable As New DataTable(GetType(T).Name)
        Dim Properties As PropertyInfo() = GetType(T).GetProperties(BindingFlags.[Public] Or BindingFlags.Instance)

        For Each propInfo As PropertyInfo In Properties
            dataTable.Columns.Add(propInfo.Name)
        Next

        For Each item As T In items
            Dim values = New Object(Properties.Length - 1) {}

            For i As Integer = 0 To Properties.Length - 1
                values(i) = Properties(i).GetValue(item, Nothing)
            Next
            dataTable.Rows.Add(values)
        Next
        Return dataTable
    End Function

End Class

Public Class State
    Private _Name As String = ""
    Private _Abbr As String = ""
    Private _Capital As String = ""

    Public Property Name() As String
        Get
            Return Me._Name
        End Get
        Set(ByVal value As String)
            Me._Name = value
        End Set
    End Property

    Public Property Abbr() As String
        Get
            Return Me._Abbr
        End Get
        Set(ByVal value As String)
            Me._Abbr = value
        End Set
    End Property
    Public Property Capital() As String
        Get
            Return Me._Capital
        End Get
        Set(ByVal value As String)
            Me._Capital = value
        End Set
    End Property
End Class
answer Feb 5, 2016 by Shivaranjini
...