Click here to Skip to main content
15,881,173 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Everyone i need to know how i can add the the columns of datatable to the list and display that column values of that actually i have done sample application which is working properly for if the classs structure have the properties but i ve 2 tables i don't want to create the two separate entities i need one entity which can be work properly and i will give my sample code which is for properties please help me for the list

class Patient
public int Dosage { get; set; }
public string Drug { get; set; }
public string Patient1 { get; set; }
public DateTime Date { get; set; }

class Program
static void Main(string[] args)
Patient p = new Patient();
DataTable table = GetTable();

List<Patient> lst = GetTable().ToCollection<Patient>();
foreach (Patient cn in lst)

static DataTable GetTable()

DataTable table = new DataTable();
table.Columns.Add("Dosage", typeof(int));
table.Columns.Add("Drug", typeof(string));
table.Columns.Add("Patient", typeof(string));
table.Columns.Add("Date", typeof(DateTime));
table.Rows.Add(25, "Indocin", "David", DateTime.Now);
table.Rows.Add(50, "Enebrel", "Sam", DateTime.Now);
table.Rows.Add(10, "Hydralazine", "Christoff", DateTime.Now);
table.Rows.Add(21, "Combivent", "Janet", DateTime.Now);
table.Rows.Add(100, "Dilantin", "Melanie", DateTime.Now);
return table;


public static class MyExtensionClass
       public static List<T> ToCollection<T>(this DataTable dt)
           List<T> lst = new System.Collections.Generic.List<T>();
           Type tClass = typeof(T);
           PropertyInfo[] pClass = tClass.GetProperties();
           List<DataColumn> dc = dt.Columns.Cast<DataColumn>().ToList();
           T cn;
           foreach (DataRow item in dt.Rows)
               cn = (T)Activator.CreateInstance(tClass);
               foreach (PropertyInfo pc in pClass)

                       DataColumn d = dc.Find(c => c.ColumnName == pc.Name);
                       if (d != null)
                           pc.SetValue(cn, item[pc.Name], null);

           return lst;

now my problem is i want to do like this

class Patient
public int Dosage { get; set; }
pulic iList<string>lst=new system.generic.List<string>();

instead of that 3 properties i have to use the list and the above work has to be happen can anyone help with this please........

1 solution

do the same as this article

Convert Datatable to Collection using Generic[^]
Share this answer
MURARI00 15-Jun-11 6:00am    
Hi thanks for the response and see i don't want to add the properties to the class i just want add the properties to the list and this list contains the datacolumn headings in that list and after that from the list i ve to rettrive the data please help me

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900