Search - Articles
DevASP.NET for ASP.NET, VB.NET, XML and C# (C-Sharp) Developers Tuesday, March 03, 2009
Dev Articles
Search Directory
ASP.NET
VB.Net
C-Sharp
SQL Server
 

How to convert Generic List to DataTable in ASP.NET

Author: Rudy Pinson
Download Source Code : 1445_ConvertListDataTable.zip

I will show you in this article that how you can convert a generic list to DataTable in ASP.NET.

 

Someone asked me that how to convert generic list into DataTable. I told her that it is a piece of cake. So I wrote the following code for her and I am sharing it with you also on DevASP.NET. There are many different ways to do it but I want to keep it simple so that it will be easy for understanding also. Following piece of code will help you to convert a generic list into a DataTable.
 
See the following complete code on my page. I have added a GridView in my page for displaying result
 
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

 

   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< How to implement forgot password functionality in ASP.NET

Disclaimer - Privacy
© 2002-2017 DevASP.net