当前位置: 编程技术>.net/c#/asp.net
Asp.net GridView控件使用常用范例
来源: 互联网 发布时间:2014-08-30
本文导语: 1、前台.aspx 代码示例: || 10 15 20 30 2、后台.cs 代码示例: #region分页 protected void BindFollowExamInfoGridView(int PersonID) { int currentpage = Convert.ToInt32(lblPage.Text); DataTable dt = new DataTable(); ...
1、前台.aspx
代码示例:
||
10
15
20
30
2、后台.cs
代码示例:
#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
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
3、排序
代码示例:
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
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
1 2 3 下一页 尾页