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的显示方式。和方法一相比少了数据得制,性能也得以提升。
方法一:将 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;
}
{
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.
转载于:https://blog.51cto.com/sunday/113186