In this article, you’ll learn how to convert a List into a DataTable in C#. If you have a generic List
and you want to convert it to DataTable
without doing any manipulation, then you can use the below example:
using System;
using System.Collections.Generic;
using System.Data;
using System.Reflection;
namespace Programs
{
internal class Program
{
static void Main(string[] args)
{
List employees = new List(){
new Employee() { EmpId=1, EmpName = "Pradeep", Salary = 15000, City="Delhi" },
new Employee() { EmpId=1, EmpName = "Smith", Salary = 25000, City = "New York" },
new Employee() { EmpId=1, EmpName = "John", Salary = 21000, City = "Goa" },
new Employee() { EmpId=1, EmpName = "Tom", Salary = 25000, City = "London" },
new Employee() { EmpId=1, EmpName = "Stella", Salary = null, City = "new York" },
};
//Generic List Converted to Datatable
DataTable dt = listToDataTable(employees);
}
//This method helps you to convert List to a DataTable
public static DataTable listToDataTable(List items)
{
DataTable dataTable = new DataTable(typeof(T).Name);
//Get all the properties
PropertyInfo[] Props = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance);
foreach (PropertyInfo prop in Props)
{
//Setting column names as Property names
dataTable.Columns.Add(prop.Name, Nullable.GetUnderlyingType(prop.PropertyType) ?? prop.PropertyType);
}
foreach (T item in items)
{
var values = new object[Props.Length];
for (int i = 0; i < Props.Length; i++)
{
//inserting property values to DataTable rows
values[i] = Props[i].GetValue(item, null);
}
dataTable.Rows.Add(values);
}
return dataTable;
}
}
public class Employee
{
public int EmpId { get; set; }
public string EmpName { get; set; }
public int? Salary { get; set; }
public string City { get; set; }
}
}
I hope this article will help you to understand how to convert a List into a DataTable in C#.
Share your valuable feedback, please post your comment at the bottom of this article. Thank you!
Comments