using Infragistics.Win; using System; using System.Collections.Generic; using System.Data; using System.Linq; using System.Reflection; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace Common { public static class ExtendCls { public static ValueList ListToValuelist(this List items){ ValueList vlistScale = new ValueList(); vlistScale.ValueListItems.Add("", "全部"); vlistScale.ValueListItems.Add("0", "无效"); vlistScale.ValueListItems.Add("1", "未使用"); vlistScale.ValueListItems.Add("2", "使用中"); vlistScale.ValueListItems.Add("3", "已完成"); return vlistScale; } #region List转dataTable public static DataTable ListToDataTable(this List items) { DataTable dataTable = new DataTable(); try { PropertyInfo[] Props = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance); foreach (PropertyInfo prop in Props) { dataTable.Columns.Add(prop.Name); } foreach (T obj in items) { var values = new object[Props.Length]; for (int i = 0; i < Props.Length; i++) { values[i] = Props[i].GetValue(obj, null); } dataTable.Rows.Add(values); } return dataTable; } catch (Exception ex) { MessageBox.Show("公共方法ListToDataTable报错!错误内容:\r\n" + ex.Message); return dataTable; } } #endregion #region dataTable转List public static List TableToDataList(this DataTable table) { if (table == null) { return null; } List rows = new List(); foreach (DataRow row in table.Rows) { rows.Add(row); } return ConvertTo(rows); } private static List ConvertTo(List rows) { List list = null; if (rows != null) { list = new List(); foreach (DataRow row in rows) { T item = row.CreateItem();//CreateItem(row); list.Add(item); } } return list; } public static T CreateItem(this DataRow row) { T obj = default(T); try { if (row != null) { obj = Activator.CreateInstance(); foreach (DataColumn column in row.Table.Columns) { PropertyInfo prop = obj.GetType().GetProperty(column.ColumnName); object value = row[column.ColumnName]; if (value.ToString() != "") { if (prop.PropertyType.FullName == typeof(string).FullName) { prop.SetValue(obj, value.ToString(), null); } else if (prop.PropertyType.FullName == typeof(DateTime?).FullName) { prop.SetValue(obj, (DateTime?)Convert.ToDateTime(value.ToString()), null); } else if (prop.PropertyType.FullName == typeof(int?).FullName) { prop.SetValue(obj, (int?)Convert.ToInt32(value.ToString()), null); } else if (prop.PropertyType.FullName == typeof(long?).FullName) { prop.SetValue(obj, (long?)Convert.ToInt32(value.ToString()), null); } else if (prop.PropertyType.FullName == typeof(double?).FullName) { prop.SetValue(obj, (double?)Convert.ToDouble(value.ToString()), null); } else if (prop.PropertyType.FullName == typeof(bool).FullName) { prop.SetValue(obj, Convert.ToBoolean(value.ToString()), null); } else if (prop.PropertyType.FullName == typeof(decimal?).FullName) { prop.SetValue(obj, Convert.ToDecimal(value.ToString()), null); } //prop.SetValue(obj, value, null); } } } } catch (Exception ex) { //You can log something here //throw; } return obj; } #endregion } }