我的网页上有一个“查看”按钮。只要用户单击此按钮,就会通过Web服务获取数据,然后将数据绑定到网格视图。但是当网格视图加载时,它只显示网格的PageSize属性中提到的行数,并且不显示页码。
private void FetchData(ref DataTable dt)
{
string s_strResult = "";
string s_strQuery = "";
string s_strQueryType = "";
try
{
grdSearchResult.DataSource = dt.DataSet.Tables["Result"];
grdSearchResult.DataBind();
tblSearchResult.Visible = true;
lblSearchResult.Text = "Total Items:" + dt.DataSet.Tables["Result"].Rows.Count;
}
}
结果数据集包含5000行,每行包含30列。当gridView加载所有我能看到的只有100条记录(如PageSize = 100)。如何分页结果?只需按下按钮即可获取数据。 gridView页面索引更改的代码如下:
protected void grdSearchResult_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
try
{
grdSearchResult.PageIndex = e.NewPageIndex;
grdSearchResult.DataBind();
}
catch (Exception ex)
{
DisplayMessage(GlobalClass.enumMsgType.Error, ex.Message);
}
}
答案 0 :(得分:0)
您好Rishik请查看以下链接进行gridview分页:
http://www.codeproject.com/Articles/816953/How-To-Implement-Paging-in-GridView-Control-in-ASP
http://www.c-sharpcorner.com/UploadFile/rohatash/gridview-paging-sample-in-Asp-Net/
答案 1 :(得分:0)
您可以将数据存储在Viewstate
或Session
变量中,我建议使用属性作为这样的包装:
private DataTable dtStored
{
get { return (ViewState["dt"] == null) ? null : (DataTable)ViewState["dt"]; }
set { ViewState["dt"] = value; }
}
现在您可以绑定DataTable并将其保存在属性中:
private void FetchData(ref DataTable dt)
{
string s_strResult = "";
string s_strQuery = "";
string s_strQueryType = "";
try
{
dtStored = dt.DataSet.Tables["Result"];
grdSearchResult.DataSource = dtStored;
grdSearchResult.DataBind();
tblSearchResult.Visible = true;
lblSearchResult.Text = "Total Items:" + dt.DataSet.Tables["Result"].Rows.Count;
}
}
并在IndexPageChanging方法中添加一行:
protected void grdSearchResult_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
try
{
grdSearchResult.DataSource = dtStored;
grdSearchResult.PageIndex = e.NewPageIndex;
grdSearchResult.DataBind();
}
catch (Exception ex)
{
DisplayMessage(GlobalClass.enumMsgType.Error, ex.Message);
}
}
如果您的数据太大,可能最好将其存储在Session
变量而不是Viewstate
中,因为第二个数据将从客户端传输到服务器并从当您停留在同一页面或将变量设置为null