利用DropdownList实现DataGrid分页

  1          private   int  PageSize = 3 ;
  2          private   void  Page_Load( object  sender, System.EventArgs e)
  3          {
  4            if(!this.IsPostBack)
  5            {            
  6                this.Label1.Text="1";
  7                this.BindToDataGrid();
  8                this.BindToDDL();
  9            }

 10            // 在此处放置用户代码以初始化页面
 11        }

 12          private   void  BindToDataGrid()
 13          {
 14            SqlConnection con=new SqlConnection("server=.;database=Northwind;uid=sa;pwd=;");
 15            con.Open();
 16            SqlDataAdapter sda=new SqlDataAdapter();
 17            sda.SelectCommand=new SqlCommand("select * from Products",con);
 18            DataSet ds=new DataSet();
 19            sda.Fill(ds,"login");
 20            //实现分页
 21            System.Web.UI.WebControls.PagedDataSource ps=new PagedDataSource();
 22            ps.DataSource=ds.Tables["login"].DefaultView;
 23            ps.AllowPaging=true;
 24            ps.PageSize=3;
 25            ps.CurrentPageIndex=Convert.ToInt32(this.Label1.Text)-1;
 26            //按钮设置
 27            this.lBtnNext.Enabled=true;
 28            this.lBtnPrevious.Enabled=true;
 29            int curPage=Convert.ToInt32(this.Label1.Text);
 30            if(curPage==1)
 31            {
 32                this.lBtnPrevious.Enabled=false;
 33            }

 34            if(curPage==ps.PageCount)
 35            {
 36                this.lBtnNext.Enabled=false;
 37            }

 38            this.DataGrid1.DataSource=ps;
 39            this.DataGrid1.DataBind();
 40            con.Close();
 41        }

 42          private   void  BindToDDL()
 43          {
 44            //判断总共的行数
 45            SqlConnection con=new SqlConnection("server=.;database=Northwind;uid=sa;pwd=;");
 46            con.Open();
 47            SqlCommand cmd=new SqlCommand("select count(*) from Products",con);
 48            //判断应显示的页数
 49            int count=Convert.ToInt32(cmd.ExecuteScalar());
 50            if(count%PageSize==0)
 51            {
 52                for(int i=1;i<=count/PageSize;i++)
 53                {
 54                    SqlCommand cmd2=new SqlCommand("insert into  pageCount values("+i+","+i+")",con);
 55                    cmd2.ExecuteNonQuery();
 56                }

 57            }

 58            if(count%PageSize!=0)
 59            {
 60                for(int i=1;i<=count/PageSize+1;i++)
 61                {
 62                    SqlCommand cmd2=new SqlCommand("insert into  pageCount values("+i+","+i+")",con);
 63                    cmd2.ExecuteNonQuery();
 64                }

 65            }

 66
 67            //绑定DropDownList
 68            SqlCommand cmd3=new SqlCommand("select * from pageCount",con);
 69            SqlDataReader sdr=cmd3.ExecuteReader();
 70            this.DropDownList1.DataSource=sdr;
 71            this.DropDownList1.DataTextField="pageNum";
 72            this.DropDownList1.DataValueField="pageID";
 73            this.DropDownList1.DataBind();
 74            sdr.Close();
 75            SqlCommand cmd4=new SqlCommand("delete from pageCount where 1=1",con);
 76            cmd4.ExecuteNonQuery();
 77            con.Close();
 78        }

 79
 80          Web 窗体设计器生成的代码
104
105          private   void  DataGrid1_SelectedIndexChanged( object  sender, System.EventArgs e)
106          {
107        
108        }

109
110          private   void  lBtnNext_Click( object  sender, System.EventArgs e)
111          {
112            this.Label1.Text=Convert.ToString(Convert.ToInt32(this.Label1.Text)+1);
113            this.BindToDataGrid();
114        }

115
116          private   void  lBtnPrevious_Click( object  sender, System.EventArgs e)
117          {
118            this.Label1.Text=Convert.ToString(Convert.ToInt32(this.Label1.Text)-1);
119            this.BindToDataGrid();
120        }

121
122          private   void  DropDownList1_SelectedIndexChanged( object  sender, System.EventArgs e)
123          {
124            this.Label1.Text=this.DropDownList1.SelectedItem.Text;
125            this.BindToDataGrid();
126        }

你可能感兴趣的:(datagrid)