GridView bound to an IList<T>

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.
 
 
 
 
 
 
 
 
 
 
 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章