解决方案 »

  1.   

    只显示一个列
       DataTable的数据源是Id,Name,PYCode,我根据PYCode和Name进行筛选
       如果我将DataTable转换成IList数组,就没有问题,但是关键是绑定的数据源是DataTable的,没办法
      

  2.   

    把DataTable 转成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;
            }
      

  3.   

    使用Datatable 绑定,你过滤什么列,就应该把那个列显示出来;
      

  4.   

    把DataTable 转成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 绑定,你过滤什么列,就应该把那个列显示出来;
    我已经试过了,有用的话就不用求助了