2012-05-31 33 views
0

我有一個GridView,其中的ID是在UpdatePanel中的GridView1,我已經以編程方式定義了大部分事件,但是我沒有問題在創建事件處理程序進行排序時,您能否看看我的代碼並建議使用下面的數據表來實現分類事件的方式?GridView排序使用DataTable作爲DataSource的事件處理

// Method to BinData to GridView1 

private void BindData() 
    { 
     string strQuery = "SELECT * FROM [tbl_SignIns] "; 
     SqlCommand cmd = new SqlCommand(strQuery); 
     GridView1.DataSource = GetData(cmd); 
     GridView1.DataBind(); 
    } 

    private DataTable GetData(SqlCommand cmd) 
    { 
     DataTable dt = new DataTable(); 
     SqlConnection con = new SqlConnection(strConnString); 
     SqlDataAdapter sda = new SqlDataAdapter(); 
     cmd.CommandType = CommandType.Text; 
     cmd.Connection = con; 
     con.Open(); 
     sda.SelectCommand = cmd; 
     sda.Fill(dt); 
     return dt; 
    } 


protected void GridView1_Sorting(object sender, GridViewSortEventArgs e) 
    { 

????????????????????????????????? 

    } 
+0

檢查這個帖子:http://stackoverflow.com/questions/7594375/sort-gridview-doesnt-work希望這幫助 – BizApps

+0

謝謝,我想我的主要問題是,我可以使用當前數據表存儲在不知何故,或者我需要改變一個數據集來填充gridview呢? – rgvwed

回答

0

我有同樣的問題,我解決這個問題。首先,我在gridview header上添加了向上和向下箭頭。

 <asp:TemplateField FooterText="Toplam" HeaderText="---"> 
<HeaderTemplate> 
<asp:Label ID="Label4" runat="server" Text="---"></asp:Label><br /> 
<br /> 
<div id="myelement"> 
<asp:ImageButton ID="btnDateUp" runat="server" ImageUrl="~/Admin/skins/Skin_1/images/grid_uparrow.png" 
OnClick="btnDateUp_Click" /> 
<asp:ImageButton ID="btnDateDown" runat="server" ImageUrl="~/Admin/skins/Skin_1/images/grid_downarrow.png" 
OnClick="btnDateDown_Click" Style="width: 16px" /> 
</div> 
</HeaderTemplate> 
<ItemTemplate> 
<asp:Label ID="Label3" runat="server" Text='<%# Bind("OrderDate", "{0:D}") %>'></asp:Label> 
</ItemTemplate> 
<ItemStyle HorizontalAlign="Left" Width="210px" /> 
</asp:TemplateField> 

之後,我準備好了像這樣的sql查詢;

protected void btnDateUp_Click(object sender, ImageClickEventArgs e) 
    { 
     sortingQuery = " ORDER BY OrderDate DESC "; 
     if (radioListReport.SelectedValue == "1") 
      SortGrid(); 
     else 
      SortQuarterGrid(); 

     updatePanelGrid.Update(); 
    } 

protected void btnDateDown_Click(object sender, ImageClickEventArgs e) 
    { 
     sortingQuery = " ORDER BY OrderDate ASC "; 
     if (radioListReport.SelectedValue == "1") 
      SortGrid(); 
     else 
      SortQuarterGrid(); 

     updatePanelGrid.Update(); 
    } 

我希望它會幫助你。

0

您必須創建兩個視圖狀態,一個用於排序方向,另一個用於排序表達式,以便在排序時進行檢查。

首先,從gridview中檢索Sort Direction爲「Ascending」和「Descending」,並且當您想要在數據表中包含數據時,必須使用通過gridview排序方向的「ASC」和「DESC」在對數據進行排序時它是值得的。第二,一個翻轉功能,它將傳遞一個排序方向並將其翻轉,當它在第一次點擊時會被使用,例如第二次點擊它時,它會對「ASC」進行排序,它必須是「DESC」這就是爲什麼我翻轉它。

三,查看狀態用於保留上一類的值。

第四,創建一個viewstate來保存PageIndexChanging事件的排序數據表,當然你必須檢查viewstate是否爲null,那麼你將不得不提取數據並綁定它,否則你將不得不綁定gridview和排序數據表。

protected void GridView_Users_Sorting(object sender, GridViewSortEventArgs e) 
{ 
    if (String.IsNullOrEmpty(ViewState["sortExpression"].ToString()) && String.IsNullOrEmpty(ViewState["sortDirection"].ToString())) 
    { 
     //Sort and bind data as ASCENDING for the first time 
     DefaultSortBind(e); 
    } 
    else 
    { 
     if (ViewState["sortExpression"].ToString() == e.SortExpression.ToString()) 
     { 
      string sortDirection = string.Empty; 
      if (ViewState["sortDirection"].ToString() == e.SortDirection.ToString()) 
      { 
       sortDirection = flipSortDirection(GetSortDirection(e.SortDirection.ToString())); 
      } 
      else 
      { 
       sortDirection = GetSortDirection(e.SortDirection.ToString()); 
      } 

      DataTable dt = new UserInfoTableTableAdapter().GetData(); 
      dt.DefaultView.Sort = e.SortExpression + " " + sortDirection; 
      GridView_Users.DataSource = dt.DefaultView; 
      GridView_Users.DataBind(); 
      ViewState["sortedDt"] = dt.DefaultView.ToTable(); 
      ViewState["sortDirection"] = sortDirection; 
     } 
     else 
     { 
      //Sort and bind data as ASCENDING 
      DefaultSortBind(e); 
     } 
    } 
} 

    private void DefaultSortBind(GridViewSortEventArgs e) 
{ 
    ViewState["sortExpression"] = e.SortExpression; 
    ViewState["sortDirection"] = e.SortDirection; 
    //Fetch data again, because if we try to get data from gridview it will give null 
    DataTable dt = new UserInfoTableTableAdapter().GetData(); 
    dt.DefaultView.Sort = e.SortExpression + " " + GetSortDirection(e.SortDirection.ToString()); 
    GridView_Users.DataSource = dt.DefaultView; 
    GridView_Users.DataBind(); 
    ViewState["sortedDt"] = dt.DefaultView.ToTable(); 
} 

    //Get sort direction 
private string GetSortDirection(string sortDirection) 
{ 
    if (sortDirection == "Ascending") 
    { 
     return "ASC"; 
    } 
    else 
    { 
     return "DESC"; 
    } 
} 

//Flip sort direction 
private string flipSortDirection(string sortDirection) 
{ 
    if (sortDirection == "ASC") 
    { 
     return "DESC"; 
    } 
    else 
    { 
     return "ASC"; 
    } 
} 
相關問題