2010-04-13 32 views
1

下面的代碼示例是排序GridView形成與DataSet排序GridView形成與數據集

來源:http://www.highoncoding.com/Articles/176_Sorting_GridView_Manually_.aspx

但它不顯示任何輸出。 在sql連接中沒有問題。 我無法追查錯誤,請幫助我。 謝謝。

public partial class _Default : System.Web.UI.Page 
{ 

    private const string ASCENDING = " ASC"; 
    private const string DESCENDING = " DESC"; 

    private DataSet GetData() 
    { 
     SqlConnection cnn = new SqlConnection("Server=localhost;Database=Northwind;Trusted_Connection=True;"); 
     SqlDataAdapter da = new SqlDataAdapter("SELECT TOP 5 firstname,lastname,hiredate FROM EMPLOYEES", cnn); 
     DataSet ds = new DataSet(); 
     da.Fill(ds); 
     return ds; 
    } 

    public SortDirection GridViewSortDirection 
    { 
     get 
     { 
      if (ViewState["sortDirection"] == null) 
       ViewState["sortDirection"] = SortDirection.Ascending; 
      return (SortDirection)ViewState["sortDirection"]; 
     } 
     set { ViewState["sortDirection"] = value; } 
    } 

    protected void GridView1_Sorting(object sender, GridViewSortEventArgs e) 
    { 
     string sortExpression = e.SortExpression; 
     if (GridViewSortDirection == SortDirection.Ascending) 
     { 
      GridViewSortDirection = SortDirection.Descending; 
      SortGridView(sortExpression, DESCENDING); 
     } 
     else 
     { 
      GridViewSortDirection = SortDirection.Ascending; 
      SortGridView(sortExpression, ASCENDING); 
     } 
    } 

    private void SortGridView(string sortExpression, string direction) 
    { 
     // You can cache the DataTable for improving performance 
     DataTable dt = GetData().Tables[0]; 
     DataView dv = new DataView(dt); 
     dv.Sort = sortExpression + direction; 
     GridView1.DataSource = dv; 
     GridView1.DataBind(); 
    } 
} 

aspx頁面

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

/asp:GridView> 
+0

在問題中的引用鏈接幫助我實現什麼我試圖用一個問題來做,GridView控件不返回任何行。解決方案是將「AutoGenerateColumns」屬性設置爲true。這適用於我的情況,因爲我不想隱藏任何列。 – Doreen 2017-01-18 22:28:57

回答

0

問題是頁面加載事件時填寫的數據添加這裏面如果(!的IsPostBack)的條件。

0
//Code Behind 
    DataSet ds = new DataSet(); 
    protected void Page_Load(object sender, EventArgs e) 
      { 

       if (!IsPostBack) 
    { 
    gvbind(); 
    } 
} 

    protected DataSet gvbind() // Binding the gridview using Dataset and I am using stored procedure "Proc_Displayinfo" 
      { 
       con.Open(); 
       SqlCommand command = new SqlCommand("Proc_Displayinfo", con); 
       SqlDataAdapter adpt = new SqlDataAdapter(command); 
       DataSet ds = new DataSet(); 
       adpt.Fill(ds); 
       GridView1.DataSource = ds.Tables[0]; 
       GridView1.DataBind(); 
       con.Close(); 
       return ds; 

      } 



    public SortDirection dir 
     { 
      get 
      { 
       if (ViewState["dirState"] == null) 
       { 
        ViewState["dirState"] = SortDirection.Ascending; 
       } 
       return (SortDirection)ViewState["dirState"]; 
      } 
      set 
      { 
       ViewState["dirState"] = value; 
      } 

     } 


      protected void Gridview1_Sorting(object sender, GridViewSortEventArgs e) 
      { 


       gvbind(); 
       DataTable dt = gvbind().Tables[0]; 

      { 
       string SortDir = string.Empty; 
       if (dir == SortDirection.Ascending) 
       { 
        dir = SortDirection.Descending; 
        SortDir = "Desc"; 
       } 
       else 
       { 
        dir = SortDirection.Ascending; 
        SortDir = "Asc"; 
       } 
       DataView sortedView = new DataView(dt); 
       sortedView.Sort = e.SortExpression + " " + SortDir; 
       GridView1.DataSource = sortedView; 
       GridView1.DataBind(); 
      } 
     } 

    // Source Code 

    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="StudentId" onsorting="Gridview1_Sorting" AllowSorting="True"> 

    <asp:TemplateField HeaderText="StudentID" SortExpression="StudentID"> 
           <ItemTemplate> 
            <asp:Label ID="LBLStudentID" runat="server" Text='<%# Eval("StudentID") %>'></asp:Label> 
           </ItemTemplate> 
           <EditItemTemplate> 
            <asp:TextBox ID="TXTStudentID" runat="server" Text='<%# Eval("StudentID") %>'></asp:TextBox> 
           </EditItemTemplate>