2009-08-08 65 views
4

我想在网格视图中排序功能,但它不工作。可以帮助一些身体帮助?在gridview中排序不工作

代码:

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即将null.why?请帮助感谢。

编辑:

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"> 

EDIT(总码)

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

回答

6

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

你能否提供一些解决问题和你的代码?你的代码对某些人来说可能看起来很明显,但目前对我来说也可能不是这样。 – 2014-01-06 10:36:19

0

因为你设置一个DataSetDataSource,然后将其与运营商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是一个已经设置完全相同的对象的引用作为您的网格的数据源。它应该做的伎俩 - 如果没有那里smt其他我丢失(如我们正在排序错误的表,意味着你有多个表中的数据集)。

编辑:

看了一下你的代码。我不清楚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(); 
    } 

    } 

然后在你的排序方法,您检索的数据源是这样的:

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

您也可以检索来自分页处理程序方法中会话的DataSet。

你应该注意到性能的改进,因为你只需要从db中获取一些东西。

给它一个镜头!

+0

那么如何克服和运行代码的任何建议? – Wondering 2009-08-08 11:24:03

+0

尝试并将代码更改为DataSet ds = FillDataSet(); DataTable dt = ds.Tables [0]; if(dt!= null) {......} 现在dv.Sort的值为“Id ASc”[Id的col名称] ..但排序仍然不起作用:-( – Wondering 2009-08-08 11:30:38

+0

排序这将工作: myDataTable.DefaultView.Sort =“myColumnOfChoice DESC”; 你需要排序的数据表的默认视图 – JohnIdol 2009-08-08 11:34:40

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">