2012-05-22 77 views
0

請參閱下面的代碼,我嘗試通過調用微軟vs 2008提供的函數對網格視圖數據進行排序,但分頁完成但排序過程不起作用,請告訴我應該在哪裏我改變了下面的代碼,是的,我把網格視圖更新爲刑罰,是否有更新刑罰的問題?如何排序gridview數據

protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e) 
{ 
    GridView1.PageIndex = e.NewPageIndex; 
    showData(); 
} 
protected void GridView1_Sorting(object sender, GridViewSortEventArgs e) 
{ 
    try 
    { 
     SqlCommand cmd = new SqlCommand("showData", con); 
     cmd.CommandType = CommandType.StoredProcedure; 
     SqlDataAdapter da = new SqlDataAdapter(cmd); 
     DataSet ds = new DataSet(); 
     da.Fill(ds); 
     if (ds.Tables[0] != null) 
     { 

      ds.Tables[0].DefaultView.Sort = e.SortExpression + " " + sortType(e.SortDirection); 
      GridView1.DataSource = ds; 
      GridView1.DataBind(); 
     } 
    } 
    catch (Exception ex) 
    { 
     Label1.Text = ex.ToString(); 
    } 
} 


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

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

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

    return newSortDirection; 
} 

回答

0
+0

是我沒有AllowSorting =「true」和OnSorting =「GridView1_Sorting」 – Dadu

+0

我使用分頁和排序在同一網格視圖是否有可能在asp.net – Dadu

0

我認爲你必須分配給數據視圖的GridView的DataSource屬性,而不是數據集對象。像這樣

DataTable dt = ds.Tables[0]; 

DataView dv = new DataView(dt); 
dv.Sort = e.SortExpression + " " + sortType(e.SortDirection); 
GridView1.DataSource = dv; 
GridView1.DataBind(); 
-1

您缺少newSortDirection的默認值。

private string sortType(SortDirection sortDirection) 
{ 
    string newSortDirection = "ASC"; 

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

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

    return newSortDirection; 
} 
+0

出了什麼問題我的答案?請解釋一下,以便我可以從我的錯誤中學習。 –