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)
{
Console.WriteLine(cn.Drug);
}
Console.Read();
}
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);
}
lst.Add(cn);
}
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........