C# - How do you convert a List into DataTable

Using the following method you can convert the List into DataTable

For example you have the following List:

     /// <summary>
    /// RealTimeSalesInput 
    /// </summary>
    public List<RealTimeSales> RealTimeSalesInput { get; set; }
    /// <summary>
    /// RealTimeSales
    /// </summary>
    public class RealTimeSales
        public long StoreID { get; set; }

        public string StoreNumber { get; set; }

        public decimal Value { get; set; }

Now you can call the following function to provide the DataTable from the given List.

System.Collections.Generic.List<RealTimeSales> sales = new List<RealTimeSales>(){
                new RealTimeSales() { StoreID = 100, StoreNumber = "A100", Value = 100 },
                 new RealTimeSales() { StoreID = 101, StoreNumber = "B100", Value = 101},
                new RealTimeSales() { StoreID = 102, StoreNumber = "C100", Value = 102 }
System.Data.DataTable resultTable = ConvertToDataTableFromList(sales);

        /// <summary>
        /// Converts to data table from list.
        /// Author: Himasagar Kutikuppala.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="data">The data.</param>
        /// <returns></returns>
        public DataTable ConvertToDataTableFromList<T>(IList<T> data) 
            System.Data.DataTable table = new System.Data.DataTable();
                System.ComponentModel.PropertyDescriptorCollection props = 

                for (int i = 0; i < props.Count; i++)
                    System.ComponentModel.PropertyDescriptor prop = props[i];
                    table.Columns.Add(prop.Name, prop.PropertyType);
                object[] values = new object[props.Count];
                foreach (T item in data)
                    for (int i = 0; i < values.Length; i++)
                        values[i] = props[i].GetValue(item);
            catch (Exception ex)
                //Exception Occured 
            return table;

What is GST

GST (Goods and Service Tax) is a comprehensive tax on the supply of goods and services at each stage of any transaction. Read More

Income Tax Information

An income tax is a tax imposed by government on income earned by you. Income tax is a key source of funds that the government uses to fund its activities and serve the public. Read More

General Insurance Companies in India

General insurance is insurance for valuables other than our life and health. General insurance covers the insurer against damage, loss and theft of your valuables. Read More

Types of Bank Loans In India

Loan means lending money from one individual or entity to another. A loan has three components – principal or the borrowed amount, rate of interest and tenure or duration for which the loan is availed. Read More

List of Banks in India

The Reserve Bank of India is the central Bank that is fully owned by the Government of India. It is governed by a central board (headed by a Governor) appointed by the Central Government. Read More

List of Educational Institutions in India

The following list of comprehensive websites on higher education in India. These websites will provide detailed information on education system in India. Read More