无法对搜索的网格视图进行分页/排序

本文关键字:分页 排序 视图 网格 搜索 | 更新日期: 2023-09-27 18:37:21

几天来一直在尝试解决这个网格视图问题。

我得到了 2 个不同的网格视图,其中包含不同的信息,可以通过单击标题对其进行排序。还有一个分页功能来组织信息。除此之外,我还有一个搜索按钮,我可以在其中搜索网格视图中存在的所有信息。但是,仅对于page_load网格视图,页面/排序功能有效,但搜索的网格视图无法分页/排序。

我将向您展示如何为单个默认网格视图执行页面/排序功能。

首先,我需要将这些网格视图绑定到这样的数据集下,并在页面加载时显示信息

Session["gridview"] = DataBindByDataSet();
GVPolice.DataSource = Session["gridview"];
GVPolice.DataBind();
using (var connAdd = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString))
            {
                connAdd.Open();
                var sql = "select policeid as [Police ID], fullname as [Full Name], contact as [Contact], email as [Email], nric as [NRIC],  address as [Address], handle as [HandleCase], postedto as [Posted To] from PoliceAccount where status='available'";
                using (var cmdAdd = new SqlDataAdapter(sql, connAdd))
                {
                    DataSet dsSel = new DataSet();
                    cmdAdd.Fill(dsSel);
                    GVPolice.DataSource = dsSel;
                    GVPolice.DataBind();
                }

然后,我将网格视图绑定到另一个称为数据表的方法下

private DataTable DataBindByDataSet()
    {
        SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
                //conn.ConnectionString = "Data Source = localhost; Initial Catalog = MajorProject; Integrated Security= SSPI";
                conn.Open();
                DataSet ds = new DataSet();
                SqlDataAdapter da = new SqlDataAdapter("select policeid as [Police ID], fullname as [Full Name], contact as [Contact], email as [Email], nric as [NRIC],  address as [Address], handle as [HandleCase], postedto as [Posted To] from PoliceAccount where status='available'", conn);
                da.Fill(ds);
                conn.Close();
                 return ds.Tables[0];               
   }

下面基本上是我对页面加载时显示的数据进行排序的方式

protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
    {
        //Retrieve the table from the session object.
        DataTable dt = Session["gridview"] as DataTable;
        if (dt != null)
        {
            //Sort the data.
            dt.DefaultView.Sort = e.SortExpression + " " + GetSortDirection(e.SortExpression);
            // dt.DefaultView.Sort = e.SortExpression.ToString();
            this.GVPolice.DataSource = Session["gridview"];
            GVPolice.DataBind();
        }
    }
    private string GetSortDirection(string column)
    {
        // By default, set the sort direction to ascending.
        string sortDirection = "ASC";
        // Retrieve the last column that was sorted.
        string sortExpression = ViewState["SortExpression"] as string;
        if (sortExpression != null)
        {
            // Check if the same column is being sorted.
            // Otherwise, the default value can be returned.
            if (sortExpression == column)
            {
                string lastDirection = ViewState["SortDirection"] as string;
                if ((lastDirection != null) && (lastDirection == "ASC"))
                {
                    sortDirection = "DESC";
                }
            }
        }
        // Save new values in ViewState.
        ViewState["SortDirection"] = sortDirection;
        ViewState["SortExpression"] = column;
        return sortDirection;
    }

这是分页明智的

    protected void GVPolice_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GVPolice.PageIndex = e.NewPageIndex;
        GVPolice.DataBind();
    }

这就是我搜索信息的方式

protected void btnSearch_Click(object sender, EventArgs e)
    {
        SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
        //conn.ConnectionString = "Data Source = localhost; Initial Catalog = MajorProject; Integrated Security= SSPI";
        conn.Open();
        DataSet ds = new DataSet();
        SqlDataAdapter da = new SqlDataAdapter("select policeid as [Police ID], fullname as [Full Name], contact as [Contact], email as [Email], nric as [NRIC],  address as [Address], handle as [HandleCase], postedto as [Posted To] from PoliceAccount where status='available' and " + ddlCategory.SelectedItem.Text + " like '%" + txtData.Text + "%'", conn);
        da.Fill(ds);
        GVPolice.DataSource = ds.Copy();
        GVPolice.DataBind();
        conn.Close();
    }

正如您从"搜索"按钮中看到的那样,我重新绑定了整个网格视图,我相信它应该再次调用排序/页面代码,但不幸的是它没有。实际上,当我尝试排序/分页时,它将显示显示的page_load信息。如果有人能启发我如何在完成搜索尝试时仅对搜索的网格视图进行页面/排序,我将不胜感激。

问候。

无法对搜索的网格视图进行分页/排序

确保已将页面加载代码放置在 if 块中,如下所示

if (!IsPostBack)
{
    Session["gridview"] = DataBindByDataSet();
    GVPolice.DataSource = Session["gridview"];
    GVPolice.DataBind();
    using (var connAdd = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString))
    {
        connAdd.Open();
        var sql = "select policeid as [Police ID], fullname as [Full Name], contact as [Contact], email as [Email], nric as [NRIC],  address as [Address], handle as [HandleCase], postedto as [Posted To] from PoliceAccount where status='available'";
        using (var cmdAdd = new SqlDataAdapter(sql, connAdd))
        {
            DataSet dsSel = new DataSet();
            cmdAdd.Fill(dsSel);
            GVPolice.DataSource = dsSel;
            GVPolice.DataBind();
        }
        .....
        .....
    }
}

btnSearch_Click 事件中,按如下方式将筛选的数据存储在会话中,因为您正在使用Session["gridview"]对事件中的数据进行排序GridView1_Sorting

protected void btnSearch_Click(object sender, EventArgs e)
{
    .......
    .......
    GVPolice.DataSource = ds.Copy();
    GVPolice.DataBind();
    Session["gridview"] = ds.Tables[0];
    conn.Close()
}

现在我们将能够对过滤后的数据进行排序。

为了使分页能够正确处理过滤后的数据,我们需要更改GVPolice_PageIndexChanging事件,如下所示。

protected void GVPolice_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
    GVPolice.PageIndex = e.NewPageIndex;
    GVPolice.DataSource = Session["gridview"];
    GVPolice.DataBind();
}