20078888

技术前线

  博客园 :: 首页 :: 新随笔 :: 联系 :: 订阅 :: 管理 ::

前台:

 

代码
 <webdiyer:AspNetPager ID="AspNetPager_DataList_show" runat="server" AlwaysShow="True"
                        CustomInfoHTML
="正在查看第%CurrentPageIndex% 页/共%PageCount%页" CustomInfoSectionWidth="30%"
                        CustomInfoTextAlign
="Left" Direction="LeftToRight" Height="17px" HorizontalAlign="Center"
                        NavigationButtonType
="Image" NumericButtonType="Image" OnPageChanged="AspNetPager_DataList_show_PageChanged"
                        PageIndexBoxType
="DropDownList" PageSize="10" PagingButtonSpacing="8px" ShowBoxThreshold="10"
                        ShowCustomInfoSection
="Left" ShowPageIndexBox="Always" SubmitButtonText="查看"
                        Width
="602px">
                    
</webdiyer:AspNetPager>

 

 

后台:

 

代码
 protected void Page_Load(object sender, EventArgs e)
    {
     
if (!IsPostBack)        {
         bind();
         }
    }

  
protected void bind()
    {
        user c 
= new user();
        List
<userInfo> cinfo = c.GetAllList();
        DataTable dt 
= ToorClass.ConvertToDataTable(cinfo); //注意:这里用了一个转换

        AspNetPager_DataList_show.RecordCount 
= dt.Rows.Count;
        dt 
= GetPagedTable(dt, AspNetPager_DataList_show.CurrentPageIndex, AspNetPager_DataList_show.PageSize);

        brandstitle.DataSource 
= dt;
        brandstitle.DataBind();
    }

    
protected void AspNetPager_DataList_show_PageChanged(object sender, EventArgs e)
    {
        bind();
    }

    
/*表格分页*/
    
public static DataTable GetPagedTable(DataTable dt, int PageIndex, int PageSize)
    {
        
if (PageIndex == 0)

            
return dt;
        DataTable newdt 
= dt.Copy();
        newdt.Clear();
        
int rowbegin = (PageIndex - 1* PageSize;
        
int rowend = PageIndex * PageSize;

        
if (rowbegin >= dt.Rows.Count)
            
return newdt;

        
if (rowend > dt.Rows.Count)
            rowend 
= dt.Rows.Count;
        
for (int i = rowbegin; i <= rowend - 1; i++)
        {
            DataRow newdr 
= newdt.NewRow();
            DataRow dr 
= dt.Rows[i];
            
foreach (DataColumn column in dt.Columns)
            {
                newdr[column.ColumnName] 
= dr[column.ColumnName];
            }
            newdt.Rows.Add(newdr);
        }

        
return newdt;
    }

 

用到的转换类:

 

代码
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Reflection;
namespace SYS.Publiclass
{
   
public class ToorClass
    {
       
public ToorClass()
       { }
        
/// <summary>
        
/// Ilist<T> 转换成 DataSet
        
/// </summary>
        
/// <param name="list"></param>
        
/// <returns></returns>
        public static DataSet ConvertToDataSet<T>(IList<T> i_objlist)
        {
            
if (i_objlist == null || i_objlist.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 i_objlist)
            {
                
if (t == null)
                {
                    
continue;
                }

                row 
= dt.NewRow();

                
for (int i = 0, j = myPropertyInfo.Length; i < j; i++)
                {
                    System.Reflection.PropertyInfo pi 
= myPropertyInfo[i];

                    
string name = pi.Name;

                    
if (dt.Columns[name] == null)
                    {
                        column 
= new DataColumn(name, pi.PropertyType);
                        dt.Columns.Add(column);
                    }

                    row[name] 
= pi.GetValue(t, null);
                }

                dt.Rows.Add(row);
            }

            ds.Tables.Add(dt);

            
return ds;
        }
        
/// <summary>
        
/// Ilist<T> 转换成 DataTable
        
/// </summary>
        
/// <param name="list"></param>
        
/// <returns></returns>
        public static DataTable ConvertToDataTable<T>(IList<T> i_objlist)
        {
            
if (i_objlist == null || i_objlist.Count <= 0)
            {
                
return null;
            }
            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 i_objlist)
            {
                
if (t == null)
                {
                    
continue;
                }

                row 
= dt.NewRow();

                
for (int i = 0, j = myPropertyInfo.Length; i < j; i++)
                {
                    System.Reflection.PropertyInfo pi 
= myPropertyInfo[i];

                    
string name = pi.Name;

                    
if (dt.Columns[name] == null)
                    {
                        column 
= new DataColumn(name, pi.PropertyType);
                        dt.Columns.Add(column);
                    }

                    row[name] 
= pi.GetValue(t, null);
                }

                dt.Rows.Add(row);
            }
            
return dt;
        }
        
///**///<summary>
        
///// Ilist 转换成 DataTable
        
///// </summary>
        
///// <param name="list"></param>
        
///// <returns></returns>
        //public static DataTable ConvertToDataTable(IList i_objlist)
        
//{
        
//    if (i_objlist == null || i_objlist.Count <= 0)
        
//    {
        
//        return null;
        
//    }
        
//    DataTable dt = new DataTable();
        
//    DataRow row;

        
//    System.Reflection.PropertyInfo[] myPropertyInfo = i_objlist[0].GetType().GetProperties();

        
//    foreach (System.Reflection.PropertyInfo pi in myPropertyInfo)
        
//    {
        
//        if (pi == null)
        
//        {
        
//            continue;
        
//        }
        
//        dt.Columns.Add(pi.Name, System.Type.GetType(pi.PropertyType.ToString()));
        
//    }
        
//    for (int j = 0; j < i_objlist.Count; j++)
        
//    {
        
//        row = dt.NewRow();
        
//        for (int i = 0; i < myPropertyInfo.Length; i++)
        
//        {
        
//            System.Reflection.PropertyInfo pi = myPropertyInfo[i];
        
//            row[pi.Name] = pi.GetValue(t, null);
        
//        }
        
//        dt.Rows.Add(row);
        
//    }
        
//    return dt;
        
//}
        ///**///<summary>
        
///// Ilist 转换成 DataSet
        
///// </summary>
        
///// <param name="list"></param>
        
///// <returns></returns>
        //public static DataSet ConvertToDataSet(IList i_objlist)
        
//{
        
//    if (i_objlist == null || i_objlist.Count <= 0)
        
//    {
        
//        return null;
        
//    }
        
//    DataSet ds = new DataSet();
        
//    DataTable dt = new DataTable();
        
//    DataRow row;

        
//    System.Reflection.PropertyInfo[] myPropertyInfo = i_objlist[0].GetType().GetProperties();

        
//    foreach (System.Reflection.PropertyInfo pi in myPropertyInfo)
        
//    {
        
//        if (pi == null)
        
//        {
        
//            continue;
        
//        }
        
//        dt.Columns.Add(pi.Name, System.Type.GetType(pi.PropertyType.ToString()));
        
//    }
        
//    for (int j = 0; j < i_objlist.Count; j++)
        
//    {
        
//        row = dt.NewRow();
        
//        for (int i = 0; i < myPropertyInfo.Length; i++)
        
//        {
        
//            System.Reflection.PropertyInfo pi = myPropertyInfo[i];
        
//            row[pi.Name] = pi.GetValue(t, null);
        
//        }
        
//        dt.Rows.Add(row);
        
//    }
        
//    ds.Tables.Add(dt);
        
//    return ds;
        
//}


    }
}

 

 

 

posted on 2010-12-08 23:18  许雪林  阅读(408)  评论(2)    收藏  举报