0

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

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;

        }
    }
}

PageLoad事件函数是:

        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 中,如代码所示。我不明白的是,为什么程序会跳转到 GridView1 并在我单击以对 GridView2 中的搜索记录进行排序时对所有记录进行排序。任何帮助都感激不尽。提前致谢。

4

1 回答 1

0

您是否在 !ISPostBack 内部使用了 gridview 绑定代码?喜欢

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

    }
于 2013-09-24T06:56:22.457 回答