如何页面gridview已经有数据绑定到它

本文关键字:数据绑定 何页面 gridview | 更新日期: 2023-09-27 18:02:08

我的网页上有一个查看按钮。只要用户单击此按钮,就会通过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;
    }
}

Result DataSet包含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);
        }
}

如何页面gridview已经有数据绑定到它

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/

您可以将数据存储在ViewstateSession变量中,我建议使用像这样的属性作为包装器:

private DataTable dtStored
{
    get { return (ViewState["dt"] == null) ? null : (DataTable)ViewState["dt"]; }
    set { ViewState["dt"] = value; }
}

现在你可以绑定你的数据表,并保存在属性:

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