List to Datatable Converter Using C#

Introduction:
This article explain you to convert List Items in C# to DataTable 
Description:
Below C# code is used to convert  any list  items to datatable using a generic method
To implement this we need to write the code like as shown below

HTML Java Script CSS:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="ListToDataSet.aspx.cs" Inherits="AngularJSCURD.Pages.ListToDataSet" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="frmListToDataSet" runat="server">
        <div style="width:400px;margin:auto;">
            <asp:GridView ID="GvEmployee" runat="server" CellPadding="4" ForeColor="#333333" GridLines="None">
                <AlternatingRowStyle BackColor="White" />
                <EditRowStyle BackColor="#2461BF" />
                <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
                <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
                <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
                <RowStyle BackColor="#EFF3FB" />
                <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
                <SortedAscendingCellStyle BackColor="#F5F7FB" />
                <SortedAscendingHeaderStyle BackColor="#6D95E1" />
                <SortedDescendingCellStyle BackColor="#E9EBEF" />
                <SortedDescendingHeaderStyle BackColor="#4870BE" />
            </asp:GridView>
        </div>
    </form>
</body>
</html>

C# Code
using AngularJSCURD.Models;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Reflection;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace AngularJSCURD.Pages
{
    public partial class ListToDataSet : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            BindGrid();

        }

        public void BindGrid()
        {
            DataTable DTable = new DataTable();

            List<Employee> objEmployeeList = new List<Employee>();

            objEmployeeList.Add(new Employee { Id = 1, Name = "Muhammad Asif", Gender = "Male", Salary = 2000, DeptId = 10 });
            objEmployeeList.Add(new Employee { Id = 2, Name = "Muhammad Nizam", Gender = "Male", Salary = 3000, DeptId = 20 });
            objEmployeeList.Add(new Employee { Id = 3, Name = "Muhammad Arif", Gender = "Male", Salary = 4000, DeptId = 30 });
            objEmployeeList.Add(new Employee { Id = 4 Name = "Muhammad Azhar", Gender = "Male", Salary = 5000, DeptId = 40 });

            DTable = ConvertToDataTable<Employee>(objEmployeeList);
            GvEmployee.DataSource = DTable;
            GvEmployee.DataBind();



        }



     
    }
}

Method Used to Convert List To DataTable :

   public DataTable ConvertToDataTable<T>(List<T> items)
        {
            DataTable dataTable = new DataTable(typeof(T).Name);

         
            PropertyInfo[] Props = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance);
            foreach (PropertyInfo prop in Props)
            {
               
                dataTable.Columns.Add(prop.Name);
            }
            foreach (T item in items)
            {
                var values = new object[Props.Length];
                for (int i = 0; i < Props.Length; i++)
                {
                    
                    values[i] = Props[i].GetValue(item, null);
                }
                dataTable.Rows.Add(values);
            }
            
            return dataTable;
        }

DEMO

ListToDataTable

Blogger Tips and TricksLatest Tips And TricksBlogger Tricks