gridview中的排序不起作用

时间:2009-08-08 11:17:13

标签: c# asp.net sorting gridview ado.net

我正在尝试在网格视图中排序功能,但它无法正常工作。可以提供一些身体帮助吗?

代码:

  private string ConvertSortDirectionToSql(SortDirection sortDirection)
{
    string newSortDirection = String.Empty;

    switch (sortDirection)
    {
        case SortDirection.Ascending:
            newSortDirection = "ASC";
            break;

        case SortDirection.Descending:
            newSortDirection = "DESC";
            break;
    }

    return newSortDirection;
}
protected DataSet FillDataSet()
{
    string source = "Database=GridTest;Server=Localhost;Trusted_Connection=yes";
    con = new SqlConnection(source);
    cmd = new SqlCommand("proc_mygrid", con);
    ds = new DataSet();
    da = new SqlDataAdapter(cmd);
    da.Fill(ds);
    GridView1.DataSource = ds;
    GridView1.DataBind();

    return ds;


}
 protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
{
    DataTable dt = GridView1.DataSource as DataTable;
    if (dt != null)
    {
        DataView dv = new DataView(dt);
        dv.Sort = e.SortExpression + " " + ConvertSortDirectionToSql(e.SortDirection);
        GridView1.DataSource = dv;
        GridView1.DataBind();
   }

这里dt即将来临。为什么?请帮助谢谢。

修改

enter code here  <asp:GridView ID="GridView1" runat="server" CellPadding="4" ForeColor="#333333" 
    GridLines="None" AllowPaging="true" AllowSorting="true" PageSize="12" 
    onpageindexchanging="GridView1_PageIndexChanging" 
    onsorting="GridView1_Sorting">

编辑(总代码)

public partial class _Default : System.Web.UI.Page 
{
    SqlConnection con;
    SqlCommand cmd;
    DataSet ds;
    SqlDataAdapter da;
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    private string ConvertSortDirectionToSql(SortDirection sortDirection)
    {
        string newSortDirection = String.Empty;

        switch (sortDirection)
        {
            case SortDirection.Ascending:
                newSortDirection = "ASC";
                break;

            case SortDirection.Descending:
                newSortDirection = "DESC";
                break;
        }

        return newSortDirection;
    }
    protected DataSet FillDataSet()
    {
        string source = "Database=GridTest;Server=Localhost;Trusted_Connection=yes";
        con = new SqlConnection(source);
        cmd = new SqlCommand("proc_mygrid", con);
        ds = new DataSet();
        da = new SqlDataAdapter(cmd);
        da.Fill(ds);
        GridView1.DataSource = ds;
        GridView1.DataBind();

        return ds;


    }
    protected void GetValues(object sender, EventArgs e)
    {
        FillDataSet();
    }

    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
       int newPagenumber = e.NewPageIndex;
       GridView1.PageIndex = newPagenumber;
       GridView1.DataSource = FillDataSet();
       GridView1.DataBind();

    }


    protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
    {

        DataSet ds = FillDataSet();
        DataTable dt = ds.Tables[0];
        if (dt != null)
        {
            dt.DefaultView.Sort = e.SortExpression + " " + ConvertSortDirectionToSql(e.SortDirection);
            GridView1.DataSource = dt;
            GridView1.DataBind();
        }

3 个答案:

答案 0 :(得分:7)

CODE:

private string GetSortDirection(string column)
{
       string sortDirection = "DESC";
       string sortExpression = ViewState["SortExpression"] as string;

       if (sortExpression != null)
       {    
            if (sortExpression == column)
            {
                string lastDirection = ViewState["SortDirection"] as string;
                if ((lastDirection != null) && (lastDirection == "DESC"))
                {
                    sortDirection = "ASC";
                }
            }
       }

       ViewState["SortDirection"] = sortDirection;
       ViewState["SortExpression"] = column;

       return sortDirection;
}

protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
{
   DataTable dt = ((DataSet)Session["myDataSet"]).Tables[0];
   dt.DefaultView.Sort = e.SortExpression + " " + GetSortDirection(e.SortExpression);
   GridView1.DataSource = dt;
   GridView1.DataBind();
}

答案 1 :(得分:0)

因为您将DataSet设置为DataSource,然后使用运营商as将其投放到DataTable

C#中的'as'运算符是暂定的强制转换 - 如果无法强制转换(类型不兼容)而不是抛出异常作为直接强制转换,'as'运算符将引用设置为null。

如果您的数据集中只有一个数据表,那么您可以得到第一个这样的元素:

ds.Tables[0];

...或使用表格的名称:

ds.Tables["myTable"];

在你的情况下你可以试试......

DataTable dt = GridView1.DataSource.Tables[0] as DataTable;

希望它有所帮助!

修改

关于排序问题(一旦获得数据表):

if (dt != null) 
{ 
   dt.DefaultView.Sort = e.SortExpression + " " + ConvertSortDirectionToSql(e.SortDirection); 
   GridView1.DataBind(); 
}

您可以这样做,因为dt是对已经为网格设置为DataSource的完全相同的对象的引用。它应该做的伎俩 - 如果没有我没有其他smt(例如我们正在排序错误的表,这意味着你在DataSet中有多个表)。

修改

查看了您的代码。我不确切知道GetValues何时被解雇但我怀疑它会导致你的问题(我认为它可能会覆盖你的排序或smt沿着这些线)。

如果你从getValues中注释掉FillDataSource并修改你的PageLoad来执行此操作:

  void Page_Load(Object sender, EventArgs e)
  { 

     // Load data only once, when the page is first loaded.
     if (!IsPostBack)
     { 
        Session["myDataSet"] = FillDataSet();
     }

  }

然后在sort方法中检索DataSource,如下所示:

DataTable dt = ((DataSet)Session["myDataSet"]).Tables[0];

您还可以在分页处理程序方法中从会话中检索DataSet。

你也应该注意到性能有所提高,因为你只需要从db中检索一次这些东西。

试一试!

答案 2 :(得分:0)

在Testing.aspx.cs

protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
{
    if (dataset != null)
    {
        datatable = dataset.Tables[0];
        DataView dataView = new DataView(datatable);
        dataView.Sort = e.SortExpression;
        GridView1.DataSource = dataView;
        GridView1.DataBind();
    }
}

在Testing.aspx中

<asp:GridView ID="GridView1" runat="server" 
              AllowSorting="True" OnSorting="GridView1_Sorting">