【问题标题】:Unable to page/sort searched gridview无法对搜索到的网格视图进行分页/排序
【发布时间】:2013-10-30 06:45:50
【问题描述】:

几天来一直在尝试解决这个 gridview 问题。

我得到了 2 个不同的网格视图,它们具有不同的信息,可以通过单击标题进行排序。还有一个分页功能来组织信息。除此之外,我还有一个搜索按钮,我可以在其中搜索网格视图中存在的所有信息。但是,只有 page_load gridview,page/sort 功能起作用,但搜索到的 gridview 无法进行 page/sort。

我将向您展示我如何为一个默认的 gridview 执行页面/排序功能。

首先,我需要在这样的数据集下绑定这些gridview,并在页面加载时显示信息

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();
                }

然后我将gridview绑定到另一个名为datatable的方法下

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 信息。如果有人能告诉我如何在搜索尝试完成时为搜索到的 gridview 启用分页/排序,我将不胜感激。

问候。

【问题讨论】:

    标签: c# asp.net sorting gridview


    【解决方案1】:

    确保您已将页面加载代码放置在 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();
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-22
      • 1970-01-01
      • 2013-04-22
      相关资源
      最近更新 更多