FineUI 官方论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

本论坛已关闭(禁止注册、发帖和回复)
请移步 三石和他的朋友们

FineUI首页 WebForms - MVC & Core - JavaScript 常见问题 - QQ群 - 十周年征文活动

FineUI(开源版) 下载源代码 - 下载空项目 - 获取ExtJS - 文档 在线示例 - 版本更新 - 捐赠作者 - 教程

升级到 ASP.NET Core 3.1,快、快、快! 全新ASP.NET Core,比WebForms还简单! 欢迎加入【三石和他的朋友们】(基础版下载)

搜索
查看: 1913|回复: 3
打印 上一主题 下一主题

Grid中的数据怎么转换为List<T>数据

[复制链接]
跳转到指定楼层
楼主
发表于 2012-11-29 09:00:14 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    如题,请大侠帮忙

沙发
发表于 2012-11-29 11:38:14 | 只看该作者
我也是新手,所以帮不了你。
板凳
发表于 2012-11-29 15:46:06 | 只看该作者
实现不难吧,遍历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:50 | 只看该作者
dim_$lift 发表于 2012-11-29 15:46
实现不难吧,遍历grid列,循环属性就可以了。。下面是参考,数据库读入list:

...

谢谢,我试试
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|FineUI 官方论坛 ( 皖ICP备2021006167号-1 )

GMT+8, 2025-5-22 14:02 , Processed in 0.044910 second(s), 18 queries , Gzip On.

Powered by Discuz! X3.4

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表