GridView bound to an IList<T>
2008-11-16 23:37
363 查看
GridView绑定IList数组内容 <% ((Object[])Container.DataItem)[0] %>
Nhibernate 查询数据返回集合为 Ilist 类型,由于 Ilist 实现了 Collection ,所以当 Ilist 绑定到 DataGridView 时,显示的字段并未按真实的顺序排序,造成显示的不适当。可以采用两种解决方法:
方法一:将 Ilist 转换成 DataSet
注: iList: 数据源
className: 类完全限定名
DllFile:className 所属的程序集名 如 :SysGUI.QdcLib.dll
public DataSet ConvertIListToDataSet(IListiList, stringclassName, stringDllFile)
{
Type TheType = null;
if (DllFile != " ")
{
Assembly Assembly1 = Assembly.LoadFrom(DllFile);
TheType = Assembly1.GetType(className);
}
else
{
TheType = Type.GetType(className);
}
string sTableName = NHibernate.Cfg.ImprovedNamingStrategy.Instance.ClassToTableName(className);
BindingFlags bindFlag = BindingFlags.Public | BindingFlags.Instance;
PropertyInfo[] pInfos = TheType.GetProperties(bindFlag);
DataSet dSet = newDataSet();
DataTable dTable = dSet.Tables.Add(sTableName);
string strColmunName = " ";
foreach (PropertyInfoinfoinpInfos)
{
strColmunName = NHibernate.Cfg.ImprovedNamingStrategy.Instance.PropertyToColumnName(info.Name);
dTable.Columns.Add(strColmunName, info.PropertyType.GetType());
}
foreach(objectobjiniList){
DataRow dRow = dTable.NewRow();
foreach(PropertyInfoinfoinpInfos){
strColmunName = NHibernate.Cfg.ImprovedNamingStrategy.Instance.PropertyToColumnName(info.Name);
dRow[strColmunName] = info.GetValue(obj, null);
}
dTable.Rows.Add(dRow);
}
return dSet;
}
上面采用反射根据属性名及类型信息生成一个DataTable,然后用Ilist数据填充DataTable,这种方法有个不好的地方就是如果数据量大的时候,严重造成了程序的性能损失。
方法二:建一个表来保存所有表的字段信息,DataGridView加载Ilist数据以后再改变DataGirdView的显示方式。和方法一相比少了数据得制,性能也得以提升。
Here is a sample which shows how to convert IList into DataSet (DataTable):
public static DataSet ConvertToDataSet(IList<T> list)
{
if (list == null || list.Count <= 0)
{
return null;
}
DataSet ds = new DataSet();
DataTable dt = new DataTable(typeof(T).Name);
DataColumn column;
DataRow row;
System.Reflection.PropertyInfo[] myPropertyInfo = typeof(T).GetProperties(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance);
foreach (T t in list)
{
if (t == null)
{
continue;
}
row = dt.NewRow();
for (int i = 0, j = propertyInfo.Length; i < j; i++)
{
System.Reflection.PropertyInfo pInfo = propertyInfo[i];
string name = pInfo.Name;
if (dt.Columns[name] == null)
{
column = new DataColumn(name, pInfo.PropertyType);
dt.Columns.Add(column);
}
row[name] = pInfo.GetValue(t, null);
}
dt.Rows.Add(row);
}
ds.Tables.Add(dt);
return ds;
}
You can sort it and bind to GridView, but it is not a good way. I suggest to use ObjectDataSource as above reply from bcanonica.
Nhibernate 查询数据返回集合为 Ilist 类型,由于 Ilist 实现了 Collection ,所以当 Ilist 绑定到 DataGridView 时,显示的字段并未按真实的顺序排序,造成显示的不适当。可以采用两种解决方法:
方法一:将 Ilist 转换成 DataSet
注: iList: 数据源
className: 类完全限定名
DllFile:className 所属的程序集名 如 :SysGUI.QdcLib.dll
public DataSet ConvertIListToDataSet(IListiList, stringclassName, stringDllFile)
{
Type TheType = null;
if (DllFile != " ")
{
Assembly Assembly1 = Assembly.LoadFrom(DllFile);
TheType = Assembly1.GetType(className);
}
else
{
TheType = Type.GetType(className);
}
string sTableName = NHibernate.Cfg.ImprovedNamingStrategy.Instance.ClassToTableName(className);
BindingFlags bindFlag = BindingFlags.Public | BindingFlags.Instance;
PropertyInfo[] pInfos = TheType.GetProperties(bindFlag);
DataSet dSet = newDataSet();
DataTable dTable = dSet.Tables.Add(sTableName);
string strColmunName = " ";
foreach (PropertyInfoinfoinpInfos)
{
strColmunName = NHibernate.Cfg.ImprovedNamingStrategy.Instance.PropertyToColumnName(info.Name);
dTable.Columns.Add(strColmunName, info.PropertyType.GetType());
}
foreach(objectobjiniList){
DataRow dRow = dTable.NewRow();
foreach(PropertyInfoinfoinpInfos){
strColmunName = NHibernate.Cfg.ImprovedNamingStrategy.Instance.PropertyToColumnName(info.Name);
dRow[strColmunName] = info.GetValue(obj, null);
}
dTable.Rows.Add(dRow);
}
return dSet;
}
上面采用反射根据属性名及类型信息生成一个DataTable,然后用Ilist数据填充DataTable,这种方法有个不好的地方就是如果数据量大的时候,严重造成了程序的性能损失。
方法二:建一个表来保存所有表的字段信息,DataGridView加载Ilist数据以后再改变DataGirdView的显示方式。和方法一相比少了数据得制,性能也得以提升。
Here is a sample which shows how to convert IList into DataSet (DataTable):
public static DataSet ConvertToDataSet(IList<T> list)
{
if (list == null || list.Count <= 0)
{
return null;
}
DataSet ds = new DataSet();
DataTable dt = new DataTable(typeof(T).Name);
DataColumn column;
DataRow row;
System.Reflection.PropertyInfo[] myPropertyInfo = typeof(T).GetProperties(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance);
foreach (T t in list)
{
if (t == null)
{
continue;
}
row = dt.NewRow();
for (int i = 0, j = propertyInfo.Length; i < j; i++)
{
System.Reflection.PropertyInfo pInfo = propertyInfo[i];
string name = pInfo.Name;
if (dt.Columns[name] == null)
{
column = new DataColumn(name, pInfo.PropertyType);
dt.Columns.Add(column);
}
row[name] = pInfo.GetValue(t, null);
}
dt.Rows.Add(row);
}
ds.Tables.Add(dt);
return ds;
}
You can sort it and bind to GridView, but it is not a good way. I suggest to use ObjectDataSource as above reply from bcanonica.
相关文章推荐
- Dev GridView 绑定List<T>、BindingList <T>、BindingSource
- 扩展方法 DataTable To List<T>
- You've implemented -[<UIApplicationDelegate> application:didReceiveRemoteNotification:fetchCompletionHandler:], but you still need to add "remote-notification" to the list of your supported UIBackgrou
- List is a raw type. References to generic type List<E> should be parameterized
- <<DEITEL - Python How to Program 2002>>Special attributes of an object of a class.0.0.0.3
- list<Object> to list<String>
- How to sort a generic List<T>
- Dev GridView 绑定List<T>、BindingList <T>、BindingSource
- js Array to C# List<Dictionary>(js 传参 自定义类列表 到C#)
- <LeetCode OJ> 109 / 108 Convert Sorted ( List / Array ) to Binary Search Tree
- C#.NET IDataReader扩展 ToList<T>/Get<T>
- incompatible types: ArrayList<ArrayList<Integer>> cannot be converted to List<List<Integer>>
- how to query for a list<String> in jdbctemplate?--转载
- ToList<>()所带来的性能影响
- [测试]Gridview绑定SqlDataReader+IList<T>和SqlDataAdapter+DataSet的效率
- ToList<>()所带来的性能影响
- An Error occurred While Establishing a Connection Back to the Adminstration Server t3://<servername>
- 扩展方法 DataTable To List<T>
- How to enable sorting on a GridView using an IEnumerable list as datasource
- 【小熊刷题】Convert Sorted List to Balanced Binary Search Tree<可再复习>