asp.net的GridView控件使用方法大全

复制代码 代码如下:


<asp:Label runat="server" Text="总页数:"></asp:Label>
<asp:Label runat="server" Text=""></asp:Label>
<asp:Label runat="server" Text="当前页:"></asp:Label>
<asp:Label Text="1" runat="server"></asp:Label>&nbsp; 
<asp:LinkButton runat="server" >|&lt;</asp:LinkButton>&nbsp; 
<asp:LinkButton runat="server" >&lt;</asp:LinkButton>&nbsp; 
<asp:LinkButton runat="server" >&gt;</asp:LinkButton>&nbsp;
<asp:LinkButton runat="server" >&gt;|</asp:LinkButton>&nbsp; 
<asp:DropDownList runat="server" AutoPostBack="True" 
      OnSelectedIndexChanged="ddlPage_SelectedIndexChanged"> 
      <asp:ListItem>10</asp:ListItem> 
        <asp:ListItem>15</asp:ListItem> 
      <asp:ListItem>20</asp:ListItem> 
      <asp:ListItem>30</asp:ListItem> 
</asp:DropDownList> 
<asp:Label runat="server" Text="条/页"></asp:Label>  


后台  

复制代码 代码如下:


#region分页
protected void BindFollowExamInfoGridView(int PersonID)
  {
    int currentpage = Convert.ToInt32(lblPage.Text);
    DataTable dt = new DataTable();
    dt = feibf.GetByPersonIDFollowExamInfo(PersonID);  //查询指定人的随访信息记录
    if (dt.Rows.Count > 0)
    {
      FollowExamInfoGridView.DataSource = dt;
      FollowExamInfoGridView.DataBind();
      PagedDataSource ps = new PagedDataSource();
      ps.DataSource = dt.DefaultView;
      ps.AllowPaging = true;
      ps.PageSize = Convert.ToInt32(ddlPage.SelectedValue);
      lblPageCount.Text = ps.PageCount.ToString();
      this.lblPreButton.Enabled = true;
      this.lblNextButton.Enabled = true;
      ps.CurrentPageIndex = currentpage - 1;
      if (currentpage == 1)
      {
        this.lblPreButton.Enabled = false;
        this.lblFirstButton.Enabled = false;
      }
      else
      {
        this.lblPreButton.Enabled = true;
        this.lblFirstButton.Enabled = true;
      }
      if (currentpage == ps.PageCount)
      {
        this.lblNextButton.Enabled = false;
        this.lblLastButton.Enabled = false;
      }
      else
      {
        this.lblNextButton.Enabled = true;
        this.lblLastButton.Enabled = true;
      }
      FollowExamInfoGridView.DataSource = ps;
      FollowExamInfoGridView.DataBind();
    }
    
  }
  protected void lblPreButton_Click(object sender, EventArgs e)
  {
    this.lblPage.Text = Convert.ToString(Convert.ToUInt32(lblPage.Text) - 1);
    BindFollowExamInfoGridView(Convert.ToInt32(Request.QueryString["PersonID"]));
  }
  protected void lblNextButton_Click(object sender, EventArgs e)
  {
    this.lblPage.Text = Convert.ToString(Convert.ToUInt32(lblPage.Text) + 1);
    BindFollowExamInfoGridView(Convert.ToInt32(Request.QueryString["PersonID"]));
  }
  protected void lblFirstButton_Click(object sender, EventArgs e)
  {
    this.lblPage.Text = "1";
    BindFollowExamInfoGridView(Convert.ToInt32(Request.QueryString["PersonID"]));
  }
  protected void lblLastButton_Click(object sender, EventArgs e)
  {
    this.lblPage.Text = lblPageCount.Text;
    BindFollowExamInfoGridView(Convert.ToInt32(Request.QueryString["PersonID"]));
  }
  protected void ddlPage_SelectedIndexChanged(object sender, EventArgs e)
  {
    lblPage.Text = "1";
    BindFollowExamInfoGridView(Convert.ToInt32(Request.QueryString["PersonID"]));
  }
#endregion 

排序
Allowsort = "true"
sortExpression = "ID"
DataView dv = SortBindGrid(dt);
#region排序
  protected void FollowExamInfoGridView_Sorting(object sender, GridViewSortEventArgs e)
  {
    ViewState["sortexpression"] = e.SortExpression;
    if (ViewState["sortdirection"] == null)
    {
      ViewState["sortdirection"] = "asc";
    }
    else
    {
      if (ViewState["sortdirection"].ToString() == "asc")
      {
        ViewState["sortdirection"] = "desc";
      }
      else
      {
        ViewState["sortdirection"] = "asc";
      }
    }
   
    BindFollowExamInfoGridView(Convert.ToInt32(HiddenPersonID.Value));
  }
  public DataView SortBindGrid(DataTable table)
  {
    if (table != null)
    {
      DataView dv = table.DefaultView;
      if (ViewState["sortexpression"] != null && ViewState["sortdirection"] != null)
      {
        dv.Sort = ViewState["sortexpression"].ToString() + " " + ViewState["sortdirection"].ToString();
      }
      return dv;
    }
    else
    {
      return null;
    }
  }
  #endregion 

=======自带分页
  #region自带分页

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wjgxxs.html