过滤后的Gridview分页

时间:2012-05-07 07:33:33

标签: asp.net gridview paging

我在GridView中进行了分页,之前它运行良好:它是通过TextBox中的“用户键”值进行过滤。但是,由于我在GridView中添加了过滤功能,因此分页存在一些问题。当用户尝试单击第2页时,它将在过滤之前显示GridView的第2页。

有人能帮助我吗?下面是我的分页代码隐藏:

protected void gvPaging(object sender, GridViewPageEventArgs e)
{
    DefaultData();
    GridView1.PageIndex = e.NewPageIndex;
    GridView1.DataBind();
} 

过滤代码:

protected void Button1_Command(object sender, EventArgs e)
{    
    string folderName = ConfigurationManager.AppSettings["folderPDF"].ToString();
    string path = Server.MapPath("./");
    string fpaths = path + folderName;
    string[] filePath = Directory.GetFiles(fpaths, "*.pdf");
    DataTable table = GetTable(filePath);

    //var dataTable = (DataTable)GridView1.DataSource;
    var dataView = table.DefaultView;
    dataView.RowFilter = "folderName LIKE '" + DocSearch.Text.Trim() + "%'";

    GridView1.DataSource = table;
    GridView1.DataBind();

    DocSearch.Text = "";
}

DefaultData()

public void DefaultData()
{
    string folderName = ConfigurationManager.AppSettings["folderPDF"].ToString();
    string path = Server.MapPath("./");
    string fullPath = path + folderName;
    string[] filePaths = Directory.GetFiles(fullPath, "*.pdf");
    DataTable table = GetTable(filePaths);

    GridView1.DataSource = table;
    GridView1.DataBind();
}

2 个答案:

答案 0 :(得分:0)

您需要将filterig选项添加到网格视图分页功能中,否则它将无法工作,因为您需要告诉它应该从哪个数据源获取信息

答案 1 :(得分:0)

我真的为这个问题搞得很多,人们正在寻找我在这里找到的正确的解决方案..他们......首先在gridview中设置enablepagingandcallback = false,然后添加

Private Sub GridView1_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles GridView1.PageIndexChanging
    GridView1.PageIndex = e.NewPageIndex
    GridView1.DataSource = SqlDataSource1
    GridView1.DataBind()

End Sub

如果使用数据视图过滤器使用它而不是sqldatasource过滤表达式

,那就是全部