public static List<T> DataTableToList<T>(DataTable dt)
{
List<T> list = new List<T>();
Type type = typeof(T);
PropertyInfo[] properties = type.GetProperties();
List<PropertyInfo> listProperties = new List<PropertyInfo>();
for (int i = 0; i < dt.Columns.Count; i++)
{
var p = properties.Where(t => t.Name.ToLower() == dt.Columns[i].ColumnName.ToLower()).FirstOrDefault();
if (p != null)
{
listProperties.Add(p);
}
}
for (int i = 0; i < dt.Rows.Count; i++)
{
T t = (T)Activator.CreateInstance(type);
SetValue(t, listProperties, dt.Rows[i]);
list.Add(t);
}
return list;
}
public static DataTable ListTotable<T>(List<T> list)
{
DataTable dt = new DataTable();
Type t = typeof(T);
PropertyInfo[] propertyInfos = t.GetProperties();
foreach (var item in propertyInfos)
{
dt.Columns.Add(item.Name);
}
foreach (var item in list)
{
object[] strs = new object[dt.Columns.Count];
for (int i = 0; i < propertyInfos.Length; i++)
{
strs[i] = propertyInfos[i].GetValue(item);
}
dt.Rows.Add(strs);
}
return dt;
}
private static void SetValue<T>(T t, List<PropertyInfo> listProperties,DataRow dr)
{
foreach (var item in listProperties)
{
if (item.PropertyType == typeof(int))
item.SetValue(t, int.Parse(dr[item.Name].ToString()));
else if (item.PropertyType == typeof(double))
item.SetValue(t, double.Parse(dr[item.Name].ToString()));
else if (item.PropertyType == typeof(long))
item.SetValue(t, long.Parse(dr[item.Name].ToString()));
else if (item.PropertyType == typeof(bool))
item.SetValue(t, bool.Parse(dr[item.Name].ToString()));
else if (item.PropertyType == typeof(DateTime))
item.SetValue(t, DateTime.Parse(dr[item.Name].ToString()));
else if (item.PropertyType == typeof(char))
item.SetValue(t, char.Parse(dr[item.Name].ToString()));
else
item.SetValue(t, dr[item.Name].ToString());
}
}
public static string DatatableToBody(DataTable dt, string[] ColumnNames, string[] specColumns,
Color headerColor, Color highlineColor,Color cellFontColor
)
{
string res = "";
if (ColumnNames.Length == 0 || ColumnNames[0].Split(';').Length != 2)
{
return "Fail,ColumnNames must be split by ;";
}
if (dt != null)
{
res = res + $@"<table border=1 width ='100%' style ='text-align:center; border-color:gray; border-collapse:collapse;' display:fixed>
<tr style = 'background-color:{headerColor.Name}'> ";
for (int i = 0; i < ColumnNames.Length; i++)
{
res = res + @"<td style='width:" + ColumnNames[i].Split(';')[1] + "'> " + ColumnNames[i].Split(';')[0] + " </td>";
}
res = res + @" </tr> ";
// style = 'background-color:red'
for (int i = 0; i < dt.Rows.Count; i++)
{
res = res + " <tr> ";
for (int k = 0; k < ColumnNames.Length; k++)
{
if (specColumns != null && specColumns.Where(x => x == ColumnNames[k].ToString().Split(';')[0]).Any())
res = res + $@"<td style = 'background-color:{highlineColor.Name};color:{cellFontColor.Name}'> " + dt.Rows[i][ColumnNames[k].Split(';')[0]].ToString() + " </td>";
else
res = res + @"<td> " + dt.Rows[i][ColumnNames[k].Split(';')[0]].ToString() + " </td>";
}
res = res + @" </tr> ";
}
res = res + "</table>";
}
return res;
}
public static void GetFiles(string path,ref List<FileInfo> files,Func<string, List<FileInfo>> func =null)
{
var dirs = Directory.GetDirectories(path);
if (dirs.Length > 0)
{
if (func != null)
{
files.AddRange(func(path));
}
else
{
files.AddRange(new DirectoryInfo(path).GetFiles());
}
foreach (var item in dirs)
{
GetFiles(item, ref files, func);
}
}
else
{
if (func != null)
{
files.AddRange(func(path));
}
else
{
files.AddRange(new DirectoryInfo(path).GetFiles());
}
}
}