5 changed files with 173 additions and 136 deletions
@ -0,0 +1,63 @@ |
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.Collections.ObjectModel; |
|||
using System.Data; |
|||
using System.Linq; |
|||
using System.Reflection; |
|||
using System.Text; |
|||
using System.Threading.Tasks; |
|||
|
|||
namespace formula_manage.UserClass |
|||
{ |
|||
internal class DataTableToObservableCollection |
|||
{ |
|||
public ObservableCollection<T> ToObservableCollection<T>(DataTable dt) where T : class, new() |
|||
{ |
|||
Type t = typeof(T); |
|||
PropertyInfo[] propertys = t.GetProperties(); |
|||
ObservableCollection<T> lst = new ObservableCollection<T>(); |
|||
string typeName = string.Empty; |
|||
foreach (DataRow dr in dt.Rows) |
|||
{ |
|||
T entity = new T(); |
|||
foreach (PropertyInfo pi in propertys) |
|||
{ |
|||
typeName = pi.Name; |
|||
if (dt.Columns.Contains(typeName)) |
|||
{ |
|||
if (!pi.CanWrite) continue; |
|||
object value = dr[typeName]; |
|||
if (value == DBNull.Value) continue; |
|||
if (pi.PropertyType == typeof(string)) |
|||
{ |
|||
pi.SetValue(entity, value.ToString(), null); |
|||
} |
|||
else if (pi.PropertyType == typeof(int) || pi.PropertyType == typeof(int?)) |
|||
{ |
|||
pi.SetValue(entity, int.Parse(value.ToString()), null); |
|||
} |
|||
else if (pi.PropertyType == typeof(DateTime?) || pi.PropertyType == typeof(DateTime)) |
|||
{ |
|||
pi.SetValue(entity, DateTime.Parse(value.ToString()), null); |
|||
} |
|||
else if (pi.PropertyType == typeof(float)) |
|||
{ |
|||
pi.SetValue(entity, float.Parse(value.ToString()), null); |
|||
} |
|||
else if (pi.PropertyType == typeof(double)) |
|||
{ |
|||
pi.SetValue(entity, double.Parse(value.ToString()), null); |
|||
} |
|||
else |
|||
{ |
|||
pi.SetValue(entity, value, null); |
|||
} |
|||
} |
|||
} |
|||
lst.Add(entity); |
|||
} |
|||
return lst; |
|||
} |
|||
|
|||
} |
|||
} |
|||
Loading…
Reference in new issue