对搜索到的记录进行排序

时间:2013-09-24 05:59:16

标签: c# asp.net gridview

我在将网格视图中的搜索记录排序为上升和下降方向时遇到问题。我已经将以下代码应用于普通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中的“All-Records”,而搜索记录则放在GridView2中,如代码所示。我无法理解的是,当我点击单击以对GridView2中的搜索记录进行排序时,为什么程序会跳转到GridView1并对所有记录进行排序。任何帮助都感激不尽。提前谢谢。

1 个答案:

答案 0 :(得分: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;
        }

    }