关于DataGrid分页问题

关于DataGrid分页问题

       我总在别人面前夸VS.NET是这么的智能,是多么的为“民工”着想。他对程序员的关照绝对不亚于牛哥对织女的痴情。前天碰到了一个分页的问题,本来以为只要添加几个属性就搞定了,没想到不行。那天心里特别烦躁(估计太热的关系),我搞了好久也没搞定。一气之下去洗了把澡,突然想到我干吗不网上找答案?靠,我平时找资料很勤快的,怎么今天反映这么迟钝。
       一会儿,我就找到答案了,错就错在我太相信VS.NET的智能了。dataGrid虽然提供了翻页的功能,但是不太智能,需要你通过一个事件把新页设置成当年页。

这是datashow显示部分,注意一定要再<form>内
<asp:datagrid id="dataShow" Runat="server" OnPageIndexChanged="dataShow_Changed" AllowPaging=True PageSize=2 PagerStyle-HorizontalAlign=Right Width=100%></asp:datagrid>

代码如下:
    OleDbConnection conn;
        
string  strcon;
        
string  strcmd;
        
protected  System.Web.UI.WebControls.Label label1;
        OleDbDataAdapter cmd;
        DataSet ds;
        
private   void  Page_Load( object  sender, System.EventArgs e)
        
{
                strcon
="Provider=microsoft.jet.oledb.4.0;data source=c:\\inetpub\\wwwroot\\Flowers\\FlowerDB.mdb";
                conn
=new OleDbConnection(strcon);
                strcmd
="select FlowerName,FlowerPrice from Flowers where FlowerTypeID=(select FlowerTypeID from FlowerType where FlowerTypeName=@name)";
                cmd
=new OleDbDataAdapter(strcmd,conn);
                cmd.SelectCommand.Parameters.Add(
new OleDbParameter("@name", OleDbType.Char,80));
                cmd.SelectCommand.Parameters[
"@name"].Value=Session["name"];
                ds
=new DataSet();
                cmd.Fill(ds,
"Flowers");    
                dataShow.DataSource
=ds.Tables["Flowers"].DefaultView;    
                dataShow.DataBind();
                label1.Text
="一共有" +ds.Tables["Flowers"].Rows.Count.ToString()+"条记录。";

        }

// 这个事件就是当用户选择改变当前页的索引时,就把新页的索引作为当前页的索引,同时需要再次绑定
         public   void  dataShow_Changed(Object sender, DataGridPageChangedEventArgs e)
        
{
            dataShow.CurrentPageIndex 
= e.NewPageIndex;
            dataShow.DataBind();
            
        }



你可能感兴趣的:(关于DataGrid分页问题)