获得总记录数有两种方法:第一种方式:在绑定方法中ds.Tables[0].Rows.Count;中的表格行数获得
private void SetBind()
{
DataSet ds = new DataSet();
using (SqlConnection conn = new SqlConnection(sConnectionString))
{
SqlDataAdapter da = new SqlDataAdapter("SELECT tbMajor.MajorID, tbMajor.MajorName, tbDepartment.DepartmentName, tbUser.UserRealName FROM tbMajor LEFT OUTER JOIN tbDepartment ON tbMajor.MajorDepartmentID = tbDepartment.DepartmentID LEFT OUTER JOIN tbUser ON tbMajor.MajorHeadID = tbUser.UserID ORDER BY tbDepartment.DepartmentName, tbMajor.MajorName", conn);
da.Fill(ds);
}
ToatalRowsCount = ds.Tables[0].Rows.Count;
DataView dv = new DataView(ds.Tables[0]);
if (ViewState["SortExpresstion"] != null)
dv.Sort = ViewState["SortExpresstion"].ToString() + " " + ViewState["SortDirection"].ToString();
gv_Major.DataSource = dv;
gv_Major.DataBind();
}
第二种方式,在绑定方法中通过查询数据库的方式获得
private void SetBind()
{
DataSet ds = new DataSet();
using (SqlConnection conn = new SqlConnection(sConnectionString))
{
SqlDataAdapter da = new SqlDataAdapter("select tbReview.ReviewID,tbReview.ReviewSubmmitState, tbReview.ReviewFirstScore,tbReview.ReviewSecondScore,tbReview.ReviewThirdScore,tbReview.ReviewFourthScore,tbReview.ReviewFifthScore,tbReview.ReviewSixthScore,tbReview.ReviewSeventhScore,tbReview.ReviewSumScore,tbMajor.MajorName from tbReview left outer join tbMajor on tbReview.ReviewMajorID=tbMajor.MajorID where ReviewUserID= '"+Session["UserID"].ToString()+"' and tbReview.ReviewState='已评审'",conn);
da.Fill(ds);
conn.Open();
{
using (SqlCommand cmd = new SqlCommand("select count(*) from tbReview where ReviewUserID='" + Session["UserID"].ToString() + "' and ReviewState='已评审'", conn))
{
MajorCount = Convert.ToInt16(cmd.ExecuteScalar());
}
}
}
DataView dv = new DataView(ds.Tables[0]);
if (ViewState["SortExpresstion"] != null)
dv.Sort = ViewState["SortExpresstion"].ToString() + " " + ViewState["SortDirection"].ToString();
gv_ReviewIndex.DataSource = dv;
gv_ReviewIndex.DataBind();
}
在页脚显示记录数、当前页、总页。当前页PageIndex+1,总页:PageCount
前端:模板列的页脚模板中加一个标签用于接收数据,也可以不加。
<asp:TemplateField HeaderText="专业名称" SortExpression="MajorName">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("MajorName") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:Label ID="Label2" runat="server" Text=""></asp:Label>
</FooterTemplate>
后台代码:
protected void gv_ReviewIndex_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Footer)
{
((Label)(e.Row.FindControl("Label2"))).Text = "共有" + MajorCount + "条记录";
(e.Row.FindControl("Label3") as Label).Text = "第+(gv_ReviewIndex.PageIndex+1).ToString() + "页+ "共" + gv_ReviewIndex.PageCount + "页";
}
}