FineUI 官方论坛

标题: Grid中的数据怎么转换为List<T>数据 [打印本页]

作者: 西门吹雪    时间: 2012-11-29 09:00
标题: Grid中的数据怎么转换为List<T>数据
    如题,请大侠帮忙


作者: sjxwb    时间: 2012-11-29 11:38
我也是新手,所以帮不了你。
作者: dim_$lift    时间: 2012-11-29 15:46
实现不难吧,遍历grid列,循环属性就可以了。。下面是参考,数据库读入list<>:
  1. private static T ExecuteDataReader<T>(IDataReader reader)
  2.         {
  3.             T obj = default(T);
  4.             try
  5.             {
  6.                 Type type = typeof(T);
  7.                 //从当前程序集里面通过反射的方式创建指定类型的对象  
  8.                 obj = (T)Activator.CreateInstance(type);
  9.                 //从另一个程序集里面通过反射的方式创建指定类型的对象
  10.                 PropertyInfo[] propertyInfos = type.GetProperties();//获取指定类型里面的所有属性
  11.                 foreach (PropertyInfo propertyInfo in propertyInfos)
  12.                 {
  13.                     for (int i = 0; i < reader.FieldCount; i++)
  14.                     {
  15.                         string fieldName = reader.GetName(i);
  16.                         if (fieldName.ToLower() == propertyInfo.Name.ToLower())
  17.                         {
  18.                             //读取表中某一条记录里面的某一列信息
  19.                             object val = reader[propertyInfo.Name];
  20.                             Type propertyType = propertyInfo.PropertyType;
  21.                             if (propertyType.IsGenericType && propertyType.GetGenericTypeDefinition() == typeof(Nullable<>))
  22.                                 propertyType = propertyType.GetGenericArguments()[0];
  23.                             if (val != null && val != DBNull.Value)
  24.                                 //给对象的某一个属性赋值
  25.                                 propertyInfo.SetValue(obj, Convert.ChangeType(val, propertyType), null);
  26.                             break;
  27.                         }
  28.                     }
  29.                 }
  30.             }
  31.             catch (Exception ex)
  32.             {
  33.                 throw ex;
  34.             }
  35.             return obj;
  36.         }
复制代码


作者: 西门吹雪    时间: 2012-11-30 01:27
dim_$lift 发表于 2012-11-29 15:46
实现不难吧,遍历grid列,循环属性就可以了。。下面是参考,数据库读入list:

...

谢谢,我试试




欢迎光临 FineUI 官方论坛 (https://fineui.com/bbs/) Powered by Discuz! X3.4