对搜索到的记录进行排序不起作用

本文关键字:排序 不起作用 记录 搜索 | 更新日期: 2023-09-27 18:32:35

我在网格视图中将搜索到的记录按升序和降序方向排序时遇到问题。我已将以下代码应用于普通网格视图(所有记录),并且它工作正常,但是当我尝试从所有记录中搜索任何记录并尝试对这些搜索的记录进行排序时,以下代码既不执行任何操作,也不引发任何错误: 以下代码应用于排序:

protected void GridView2_Sorting(object sender, GridViewSortEventArgs e)
{
string sortExpression = e.SortExpression;
if (GridViewSortDirection == SortDirection.Ascending)
{
    GridViewSortDirection = SortDirection.Descending;
    SortGridView1(sortExpression, DESCENDING);
}
else
{
    GridViewSortDirection = SortDirection.Ascending;
    SortGridView1(sortExpression, ASCENDING);
}
}
private void SortGridView1(string sortExpression, string direction)
{
DataTable dt = SearchTable().Tables[0];
DataView dv = new DataView(dt);
dv.Sort = sortExpression + direction;
GridView2.DataSource = dv;
GridView2.DataBind();
 }

搜索功能代码为:

public DataSet SearchTable()
{
    string sql1 = "SELECT * from dbo.Documents1";
    bool flag = false;
    if (!txtRef.Text.Equals(""))
    {
        if (flag == false)
        {
            sql1 = sql1 + " where Ref LIKE N'%" + txtRef.Text + "%'";
            flag = true;
        }
        else
        {
            sql1 = sql1 + "  and Ref LIKE N'%" + txtRef.Text + "%'";
        }
    }
    if (!txtSubject.Text.Equals(""))
    {
        if (flag == false)
        {
            sql1 = sql1 + " where Subject LIKE N'%" + txtSubject.Text + "%'";
            flag = true;
        }
        else
        {
            sql1 = sql1 + "  and Subject LIKE N'%" + txtSubject.Text + "%'";
        }
    }
    }
    using (SqlConnection con = new SqlConnection("Data Source=MEHDI-PC''SQLEXPRESS;Initial Catalog=PIMS;Integrated Security=True"))
    {
        using (SqlCommand cmd = new SqlCommand())
        {
            cmd.Connection = con;
            cmd.CommandText = sql1 + ";";
            //cmd.CommandType = CommandType.StoredProcedure;
            con.Open();
            //dataset object to get all select statement results
            DataSet ds = new DataSet();
            //sql dataadoptor to fill dataset
            using (SqlDataAdapter adp = new SqlDataAdapter(cmd))
            {
                adp.Fill(ds);
            }
            if (con.State == ConnectionState.Open)
            {
                con.Close();
            }
            return ds;
        }
    }
}

页面加载事件函数为:

        protected void Page_Load(object sender, EventArgs e)
    {
        GridView1.DataSource = rpt.Documents1s.ToList();
        GridView1.DataBind();
        if (!IsPostBack)
        {
            BindGrid();
            MultiView1.SetActiveView(vHome);
            btnBacktoHome.Visible = false;
            lblStatus.Visible = false;
        }
    }

我不明白我哪里出错了。 当单击要排序的列时,它会将我从搜索的记录中取出,并对放置在 GridView1 中的"所有记录"进行排序,而搜索的记录放置在 GridView2 中,如代码所示。 我不明白的是,当我单击以在 GridView2 中对搜索的记录进行排序时,为什么程序会跳转到 GridView1 并在那里对所有记录进行排序。任何帮助将不胜感激。提前谢谢。

对搜索到的记录进行排序不起作用

您是否在 !ISPostBack ?喜欢

protected void Page_Load(object sender, EventArgs e)
{
   if(!IsPostBack)
    {
       //Grid Binding code 
    }
}

为您编辑页面加载代码(必须在 !IsPostBack

protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
        GridView1.DataSource = rpt.Documents1s.ToList();
        GridView1.DataBind();
            BindGrid();
            MultiView1.SetActiveView(vHome);
            btnBacktoHome.Visible = false;
            lblStatus.Visible = false;
        }
    }