Create DataTable - CSharp System.Data

CSharp examples for System.Data:DataTable

Description

Create DataTable

Demo Code


using System.Reflection;
using System.Data;
using System.ComponentModel;
using System.Collections.Generic;
using System;// ww  w .jav  a2s .c o  m

public class Main{
        public static DataTable CreateTable<T>()
        {
            Type entityType = typeof(T);
            var table = new DataTable(entityType.Name);
            var properties = TypeDescriptor.GetProperties(entityType);

            foreach (PropertyDescriptor prop in properties)
            {
                table.Columns.Add(prop.Name,
                    Nullable.GetUnderlyingType(prop.PropertyType) ?? prop.PropertyType);
            }

            return table;
        }
}

Related Tutorials