前期我们分别介绍了gridview怎么进行固定表多行数据显示,以及怎么设计花样表头。下面我们介绍怎么利用gridview进行分页显示的同时还可以进行排序显示。
显示效果
前端DEMO
<span style="font-family:Microsoft YaHei;font-size:18px;"><html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Gridview Page</title>
<link href="~/CSS/Gridview.css" rel="stylesheet" type="text/css" />
</head>
<body>
<form id="form1" runat="server">
<div id="container">
<asp:GridView ID="GridView1" ShowFooter="false" BorderColor="Black" OnRowDataBound="GridView1_RowDataBound" runat="server" AutoGenerateColumns="False" Font-Size="12px" Width="549px" AllowPaging="True" OnSorting="GridView1_Sorting" AllowSorting="True" >
<Columns>
<asp:BoundField DataField="ID" HeaderText="编号"/>
<asp:BoundField DataField="EmpID" HeaderText="账号" SortExpression="EmpID"/>
<asp:BoundField DataField="EmpRealName" HeaderText="姓名" SortExpression="EmpRealName"/>
<asp:BoundField DataField="EmpSex" HeaderText="性别" SortExpression="EmpSex"/>
<asp:BoundField DataField="EmpAddress" HeaderText="住址" SortExpression="EmpAddress" />
<asp:BoundField DataField="EmpZipCode" HeaderText="邮编" />
<asp:BoundField DataField="EmpBirthday" HeaderText="生日" DataFormatString="{0:yyyy-MM-dd}" HtmlEncode="False" />
<asp:BoundField DataField="EmpSalary" HeaderText="薪水" DataFormatString="{0:c}" HtmlEncode="False" />
</Columns>
<HeaderStyle BackColor="Azure" Font-Size="12px" HorizontalAlign="Center" />
<RowStyle HorizontalAlign="Center" />
<PagerStyle HorizontalAlign="Center" BackColor="#FFFFCC" BorderStyle="None" BorderWidth="0px" ForeColor="#330099" />
<PagerSettings Visible="False" />
</asp:GridView>
<br />
<asp:LinkButton ID="lnkbtnFrist" runat="server" OnClick="lnkbtnFrist_Click">首页</asp:LinkButton>
<asp:LinkButton ID="lnkbtnPre" runat="server" OnClick="lnkbtnPre_Click">上一页</asp:LinkButton>
<asp:Label ID="lblCurrentPage" runat="server"></asp:Label>
<asp:LinkButton ID="lnkbtnNext" runat="server" OnClick="lnkbtnNext_Click">下一页</asp:LinkButton>
<asp:LinkButton ID="lnkbtnLast" runat="server" OnClick="lnkbtnLast_Click">尾页</asp:LinkButton>
跳转到第<asp:DropDownList ID="ddlCurrentPage" runat="server" AutoPostBack="True" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
</asp:DropDownList>页
</div>
</form>
</body>
</html>
</span>
绑定数据及分页DEMO
<span style="font-family:Microsoft YaHei;font-size:18px;">public partial class GridviewPage : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
ViewState["SortOrder"] = "EmpID";
ViewState["OrderDire"] = "ASC";
bind();
}
}
/// <summary>
/// 绑定数据
/// </summary>
public void bind()
{
string sqlStr = "select * from Employee";
DataView myds = Common.dataView(sqlStr);
string sort = (string)ViewState["SortOrder"] + " " + (string)ViewState["OrderDire"];
myds.Sort = sort;
GridView1.DataSource = myds;
GridView1.DataKeyNames = new string[] { "ID" };
GridView1.DataBind();
this.ddlCurrentPage.Items.Clear();
for (int i = 1; i <= this.GridView1.PageCount; i++)
{
this.ddlCurrentPage.Items.Add(i.ToString());
}
this.ddlCurrentPage.SelectedIndex = this.GridView1.PageIndex;
}
/// <summary>
/// 在 GridView 控件中的某个行被绑定到一个数据记录时发生。此事件通常用于在某个行被绑定到数据时修改该行的内容。
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
this.lblCurrentPage.Text = string.Format("当前第{0}页/总共{1}页", this.GridView1.PageIndex + 1, this.GridView1.PageCount); //遍历所有行设置边框样式
foreach (TableCell tc in e.Row.Cells)
{
tc.Attributes["style"] = "border-color:Black";
}
//用索引来取得编号
if (e.Row.RowIndex != -1)
{
int id = GridView1.PageIndex * GridView1.PageSize + e.Row.RowIndex + 1;
e.Row.Cells[0].Text = id.ToString();
} }
/// <summary>
/// 重新绑定
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
//下拉选框值与当前页值一致
this.GridView1.PageIndex = this.ddlCurrentPage.SelectedIndex;
bind();
}
protected void lnkbtnFrist_Click(object sender, EventArgs e)
{
//第一页,当前索引为0
this.GridView1.PageIndex = 0;
bind();
}
protected void lnkbtnPre_Click(object sender, EventArgs e)
{
if (this.GridView1.PageIndex > 0)
{
//前一页,当前索引减一
this.GridView1.PageIndex = this.GridView1.PageIndex - 1;
bind();
}
}
protected void lnkbtnNext_Click(object sender, EventArgs e)
{
if (this.GridView1.PageIndex < this.GridView1.PageCount)
{
//下一页,当前页索引值加一
this.GridView1.PageIndex = this.GridView1.PageIndex + 1;
bind();
}
}
protected void lnkbtnLast_Click(object sender, EventArgs e)
{
//最后一页,将总页数赋给当前索引
this.GridView1.PageIndex = this.GridView1.PageCount;
bind();
}</span>
排序显示DEMO
<span style="font-family:Microsoft YaHei;font-size:18px;"> /// <summary>
/// 在单击某个用于对列进行排序的超链接时发生,但在 GridView 控件执行排序操作之前。此事件通常用于取消排序操作或执行自定义的排序例程。
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
{
//sortexpression为排序表达式(账号、姓名、地址等等)
string sPage = e.SortExpression;
if (ViewState["SortOrder"].ToString() == sPage)
{
//判断当前状态为升序还是降序;
if (ViewState["OrderDire"].ToString() == "DESC")
ViewState["OrderDire"] = "ASC";
else
ViewState["OrderDire"] = "DESC";
}
else
{
ViewState["SortOrder"] = e.SortExpression;
}
bind();
}</span>
感谢您的宝贵时间······