关于gridview分页的问题 35
publicvoiddataGridBind(){curPage=this.lblPageCur.Text;SqlConnectionconn=createCon();S...
public void dataGridBind() { curPage =this.lblPageCur.Text; SqlConnection conn =createCon(); SqlCommand cmd = new SqlCommand(); cmd.Connection = conn; SqlDataAdapter sda = new SqlDataAdapter(); sda.SelectCommand = cmd; DataSet ds = new DataSet(); sda.Fill(ds, "guest"); PagedDataSource pds = new PagedDataSource(); pds.AllowPaging = true; pds.PageSize = 10; pds.DataSource = ds.Tables["guest"].DefaultView; pds.CurrentPageIndex = Convert.ToInt32(curPage) - 1; this.lblPageTotal.Text = pds.PageCount.ToString(); this.Button1.Enabled=true; this.Button2.Enabled=true; if (curPage == "1") { this.Button1.Enabled = false; } if (curPage == pds.PageCount.ToString()) { this.Button2.Enabled = false; } this.DataList1.DataSource = pds; this.DataList1.DataBind();
cmd.CommandText = "select count(*) from guest"; this.lblMesTotal.Text = Convert.ToString(cmd.ExecuteScalar());
int a = pds.PageCount; for (int i = 1; i <= a; i++) { this.DropDownList1.Items.Add(i.ToString()); } }
pagecount的循环有问题出现了
请问怎么修改可以只显示一遍而不是多次循环 展开
cmd.CommandText = "select count(*) from guest"; this.lblMesTotal.Text = Convert.ToString(cmd.ExecuteScalar());
int a = pds.PageCount; for (int i = 1; i <= a; i++) { this.DropDownList1.Items.Add(i.ToString()); } }
pagecount的循环有问题出现了
请问怎么修改可以只显示一遍而不是多次循环 展开
2个回答
展开全部
设置GridView的PageAble属性,和PageSize,
在GridView的PageIndexChanging事件中,写:
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
BindGrid();
}
其中BindGrid();是你重新读取数据,并绑定GridView的方法,例子:
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter("select * from employees", @"data source=localhost;initial catalog=northwind;user id=sa;password=123456");
da.Fill(ds,"employees");
DataView dv = ds.Tables[0].DefaultView;
if (ViewState["sortexpression"] != null)
{
dv.Sort = ViewState["sortexpression"].ToString() + " " + ViewState["sortdirection"].ToString();
}
GridView1.DataSource=dv;
GridView1.DataBind();
在GridView的PageIndexChanging事件中,写:
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
BindGrid();
}
其中BindGrid();是你重新读取数据,并绑定GridView的方法,例子:
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter("select * from employees", @"data source=localhost;initial catalog=northwind;user id=sa;password=123456");
da.Fill(ds,"employees");
DataView dv = ds.Tables[0].DefaultView;
if (ViewState["sortexpression"] != null)
{
dv.Sort = ViewState["sortexpression"].ToString() + " " + ViewState["sortdirection"].ToString();
}
GridView1.DataSource=dv;
GridView1.DataBind();
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询