解决方案 »
- c# 读excel的方法
- 求crystal report 2008的下载地址
- 如何让dateTimePicker显示的日期推后一天?
- help高分求教:如何从一个字符串中取出所有符合某正则表达式的子数据串?
- 请问在开发网页时如何动态增加内容.急!
- 如何判断winform程序是在其他机器上net share运行的?
- wpf datagrid间隔色+IsMouseOver触发器变色无效
- C#难题请大家帮忙解决一下....
- serialPort DataReceived事件监听返回值。返回的是乱码
- 创建和访问xml web services的问题,在线等待。
- 关于C#线程求解,在线等!
- 请问此静态类方法是如何被调用的~请求帮助
DataTable的数据源是Id,Name,PYCode,我根据PYCode和Name进行筛选
如果我将DataTable转换成IList数组,就没有问题,但是关键是绑定的数据源是DataTable的,没办法
参见:
/// <summary>
/// 获得集合实体
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="dt"></param>
/// <returns></returns>
public static List<T> EntityList<T>(DataTable dt)
{
if (dt == null || dt.Rows.Count == 0)
{
return null;
}
List<T> list = new List<T>();
T entity = default(T);
foreach (DataRow dr in dt.Rows)
{
entity = Activator.CreateInstance<T>();
PropertyInfo[] pis = entity.GetType().GetProperties();
foreach (PropertyInfo pi in pis)
{
if (dt.Columns.Contains(pi.Name))
{
if (!pi.CanWrite)
{
continue;
}
if (dr[pi.Name] != DBNull.Value)
{
Type t = pi.PropertyType;
if (t.FullName == "System.Guid")
{
pi.SetValue(entity, Guid.Parse(dr[pi.Name].ToString()), null);
}
else
{
pi.SetValue(entity, dr[pi.Name], null);
} }
}
}
list.Add(entity);
}
return list;
}
参见:
/// <summary>
/// 获得集合实体
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="dt"></param>
/// <returns></returns>
public static List<T> EntityList<T>(DataTable dt)
{
if (dt == null || dt.Rows.Count == 0)
{
return null;
}
List<T> list = new List<T>();
T entity = default(T);
foreach (DataRow dr in dt.Rows)
{
entity = Activator.CreateInstance<T>();
PropertyInfo[] pis = entity.GetType().GetProperties();
foreach (PropertyInfo pi in pis)
{
if (dt.Columns.Contains(pi.Name))
{
if (!pi.CanWrite)
{
continue;
}
if (dr[pi.Name] != DBNull.Value)
{
Type t = pi.PropertyType;
if (t.FullName == "System.Guid")
{
pi.SetValue(entity, Guid.Parse(dr[pi.Name].ToString()), null);
}
else
{
pi.SetValue(entity, dr[pi.Name], null);
} }
}
}
list.Add(entity);
}
return list;
}
这个我知道,没什么用。使用Datatable 绑定,你过滤什么列,就应该把那个列显示出来;
我已经试过了,有用的话就不用求助了