winfrom DataSet和实体类的相互转换

最近做WInfrom项目,对表格和控件的数据绑定非常喜欢用实体类对象来解决,但是绑定以后 又怎么从控件中拿到实体类或者转换为datatable 或者dataset呢

经过在网上的搜索以及自己的改进 完成了一个转换类,分享给大家。

public class ModelHandlerA
    {
        public class ModelHandler<T> where T : new()
        {
            #region DataTable转换成实体类
            /// <summary>
            /// 填充对象列表:用DataSet的第一个表填充实体类
            /// </summary>
            /// <param name="ds">DataSet</param>
            /// <returns></returns>
            public List<T> FillModel(DataSet ds)
            {
                if (ds == null || ds.Tables[0] == null || ds.Tables[0].Rows.Count == 0)
                {
                    return null;
                }
                else
                {
                    return FillModel(ds.Tables[0]);
                }
            }
            /// <summary>  
            /// 填充对象列表:用DataSet的第index个表填充实体类
            /// </summary>  
            public List<T> FillModel(DataSet ds, int index)
            {
                if (ds == null || ds.Tables.Count <= index || ds.Tables[index].Rows.Count == 0)
                {
                    return null;
                }
                else
                {
                    return FillModel(ds.Tables[index]);
                }
            }
            /// <summary>  
            /// 填充对象列表:用DataTable填充实体类
    
/// </summary> public List<T> FillModel(DataTable dt) { if (dt == null || dt.Rows.Count == 0) { return null; } List<T> modelList = new List<T>(); foreach (DataRow dr in dt.Rows) { //T model = (T)Activator.CreateInstance(typeof(T)); T model = new T(); for (int i = 0; i < dr.Table.Columns.Count; i++) { PropertyInfo propertyInfo = model.GetType().GetProperty(dr.Table.Columns[i].ColumnName); if (propertyInfo != null && dr[i] != DBNull.Value) {
                   Type Typeof = propertyInfo.PropertyType;      //这里是获取实体类字段属性
propertyInfo.SetValue(model, Convert.ChangeType(dr[i],Typeof), null);  //然后用 changetype方法进行转换,因为dr[i]获取到的都是string类型,需要进行转换

} } modelList.Add(model); } return modelList; } /// <summary> /// 填充对象:用DataRow填充实体类 /// </summary> public T FillModel(DataRow dr) { if (dr == null) { return default(T); } //T model = (T)Activator.CreateInstance(typeof(T)); T model = new T(); for (int i = 0; i < dr.Table.Columns.Count; i++) { PropertyInfo propertyInfo = model.GetType().GetProperty(dr.Table.Columns[i].ColumnName); if (propertyInfo != null && dr[i] != DBNull.Value) propertyInfo.SetValue(model, dr[i], null); } return model; } #endregion #region 实体类转换成DataTable /// <summary> /// 实体类转换成DataSet /// </summary> /// <param name="modelList">实体类列表</param> /// <returns></returns> public DataSet FillDataSet(List<T> modelList) { if (modelList == null || modelList.Count == 0) { return null; } else { DataSet ds = new DataSet(); ds.Tables.Add(FillDataTable(modelList)); return ds; } } /// <summary> /// 实体类转换成DataTable /// </summary> /// <param name="modelList">实体类列表</param> /// <returns></returns> public DataTable FillDataTable(List<T> modelList) { if (modelList == null || modelList.Count == 0) { return null; } DataTable dt = CreateData(modelList[0]); foreach (T model in modelList) { DataRow dataRow = dt.NewRow(); foreach (PropertyInfo propertyInfo in typeof(T).GetProperties()) { dataRow[propertyInfo.Name] = propertyInfo.GetValue(model, null); } dt.Rows.Add(dataRow); } return dt; } /// <summary> /// 根据实体类得到表结构 /// </summary> /// <param name="model">实体类</param> /// <returns></returns> private DataTable CreateData(T model) { DataTable dataTable = new DataTable(typeof(T).Name); foreach (PropertyInfo propertyInfo in typeof(T).GetProperties()) { dataTable.Columns.Add(new DataColumn(propertyInfo.Name, propertyInfo.PropertyType)); } return dataTable; } #endregion }

已经用在项目中,使用时没有问题的,如果有缺陷请大家指正。

posted @ 2017-05-17 09:11  风格不同  阅读(2501)  评论(2编辑  收藏  举报