1

我有一个位于更新面板中的网格视图。当您单击列标题时,排序工作正常,但如果我转到下一页,排序就会丢失。我将提供下面的代码,如果有人知道如何摆脱这种混乱,请帮忙!

在 Page_Load 中调用 ShowGrid() 函数。我猜问题在于在分页函数中调用 ShowGrid()。

 //Show Grid based on argument type
protected void ShowGrid(string arg)
{
    //dataset to hold email addresses
    DataSet ds = new DataSet();
    try
    {
        //open connection with new connection string            
        conn.Open();

        String selectString = String.Format("SELECT * FROM hr_OnlineJobApp"
                     + " WHERE adminCategory='" + adminCategory + "'"
                     + "AND departmentApplyingFor LIKE '" + dept + "' ORDER BY {0} {1}", ViewState["sortExp"], ViewState["sortOrder"]);
        SqlCommand cmd = new SqlCommand(selectString, conn);


        SqlDataAdapter adapter = new SqlDataAdapter(cmd);
        adapter.Fill(ds);

        //bind to gridview
        GridView1.DataSource = ds;
        GridView1.DataBind();

        GridView1.PageIndex = Convert.ToInt32(ViewState["pageIndex"]);
    }
    finally
    {
        if (conn != null)
            conn.Close();
    }
}
 /**********************************************
 * Paging functionality 
 * *******************************************/
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
    GridView1.PageIndex = e.NewPageIndex;
    //reshow Grid with current type
    adminCategory = GridView1.DataKeys[0]["adminCategory"].ToString();
    ViewState["pageIndex"] = e.NewPageIndex.ToString();
    ShowGrid(adminCategory);
}

/**********************************************
 * Sorting functionality 
 * *******************************************/
protected void gridView_Sorting(object sender, GridViewSortEventArgs e)
{
    string sortExp = e.SortExpression;
    ViewState["sortExp"] = sortExp;
    string sortDir = (string)ViewState["sortOrder"];

    //Changes the sortDir
    if (ViewState["sortOrder"].ToString() == "desc")
    {
        ViewState["sortOrder"] = "asc";
    }
    else
    {
        ViewState["sortOrder"] = "desc";
    }

    try
    {
        conn.Open();
        string sql = "SELECT * FROM hr_OnlineJobApp"
                     + " WHERE adminCategory='" + adminCategory + "'"
                     + "AND departmentApplyingFor LIKE '" + dept + "' ORDER BY " + sortExp + " " + sortDir;


        SqlDataAdapter mySqlAdapter = new SqlDataAdapter(sql, conn);
        DataSet myDataSet = new DataSet();
        mySqlAdapter.Fill(myDataSet);


        GridView1.DataSource = myDataSet;
        GridView1.DataBind();
    }
    finally
    {
        conn.Close();
    }
}
4

2 回答 2

2

您正在将PageIndex0设置为Showgrid

GridView1.PageIndex = 0;

因此,您正在覆盖新值 PageIndexChanging

GridView1.PageIndex = e.NewPageIndex;

因此,您无需在 ShowGrid 中设置 PageIndex 即可解决此问题。

该问题的另一个可能原因:

也许您在每次回发时都对 GridView 进行数据绑定Page_Load。你应该只在第一次这样做。使用Page.IsPostBack属性:

protected void Page_Load(object sender, System.EventArgs e)
{
   if(!Page.IsPostBack)
       ShowGrid();
}

最后但并非最不重要的一点:只有当用户单击gridView_Sorting. 您应该应用ORDER BY同样的ShowGrid方法或仅使用一种方法来绑定您的 GridView。但你总是在date desc那里订购。

您不应该使用 DataView 来应用排序,而应该ORDER BY首先在 sql 中使用。

String sql = String.Format("SELECT * FROM hr_OnlineJobApp"
                         + " WHERE adminCategory='"+adminCategory+"'"
                         + " ORDER BY {0} {1}", sortExp, sortDir); 
SqlCommand mySqlCommand = new SqlCommand(sql, conn);
于 2012-09-18T17:34:50.430 回答
0

当您在 ShowGrid() 函数中设置 GridView1.PageIndex = 0 时,然后在 Page_Load 事件中调用该函数。因此,在每次回发时,页面索引都设置为 0,这会产生问题。您应该删除该行以解决问题。

于 2012-09-18T18:12:28.427 回答