2012-08-25 81 views
0

我有這個網格視圖,它有2個問題。無法按排序排序gridview,也不能在另一列排序?

  1. 如果我通過單擊列對其進行排序,然後再次單擊它,但不按排序順序排序。
  2. 如果有一次我與col A排序,然後點擊任何其他列,它不會再排序?

    <asp:GridView ID="grdReport" runat="server" AutoGenerateColumns="False" DataKeyNames="CustCode" 
    ShowFooter="True" EmptyDataText="No record found" PageSize="50" 
    CssClass="mGrid" onrowdatabound="grdReport_RowDataBound" 
    AllowSorting="True" onsorting="grdReport_Sorting"> 
    <Columns> 
        <asp:TemplateField HeaderText="Select"> 
         <ItemTemplate> 
          <asp:CheckBox ID="chkSelect" runat="server"/> 
         </ItemTemplate> 
        </asp:TemplateField> 
    
        <asp:TemplateField Visible="false"> 
        <ItemTemplate> 
        <asp:Label ID="lblCustCodes" runat="server" Text='<%# Eval("CustCode") %>' CssClass="grdCustName"></asp:Label> 
        </ItemTemplate> 
        </asp:TemplateField> 
    
        <asp:TemplateField HeaderText="Customer" SortExpression="Customer"> 
        <ItemTemplate> 
        <asp:HyperLink Target="_blank" Text='<%# Eval("CustomerName") %>' runat="server" ID="hplNavigate"> 
        </asp:HyperLink> 
        </ItemTemplate> 
        </asp:TemplateField> 
        <asp:BoundField DataField="QTY" HeaderText="Booked Qty" HeaderStyle-HorizontalAlign="Right" ItemStyle-HorizontalAlign="Right" 
         SortExpression="QTY"> 
         <FooterStyle HorizontalAlign="Right" /> 
         <ItemStyle HorizontalAlign="Right"></ItemStyle> 
        </asp:BoundField> 
         <asp:BoundField DataField="Volume" HeaderText="Booked Amt" HeaderStyle-HorizontalAlign="Right" ItemStyle-HorizontalAlign="Right" 
         SortExpression="Volume"> 
         <FooterStyle HorizontalAlign="Right" /> 
         <ItemStyle HorizontalAlign="Right"></ItemStyle> 
        </asp:BoundField> 
        </asp:BoundField> 
        <asp:BoundField DataField="FirstBill" HeaderText="First Bill" HeaderStyle-HorizontalAlign="left" ItemStyle-HorizontalAlign="left" 
         SortExpression="FirstBill"> 
         <FooterStyle HorizontalAlign="Left" /> 
         <ItemStyle HorizontalAlign="Left"></ItemStyle> 
        </asp:BoundField> 
    </Columns> 
    <FooterStyle BackColor="Aqua" Font-Bold="true" ForeColor="BlueViolet"/> 
    

背後排序的代碼是

 switch (e.SortExpression) 
     { 
      case "Customer": 
       if (e.SortDirection == SortDirection.Ascending) 
       { 
        var result = from table in Ob.DataTableOther.AsEnumerable() 
           orderby table.Field<string>("CustomerName") 
           select table; 
        var dv = result.AsDataView(); 
        grdReport.DataSource = dv; 
        grdReport.DataBind(); 
       } 
       else 
       { 
        var result = from table in Ob.DataTableOther.AsEnumerable() 
           orderby table.Field<string>("CustomerName") descending 
           select table; 
        var dv = result.AsDataView(); 
        grdReport.DataSource = dv; 
        grdReport.DataBind(); 
       }      
       break; 

      case "QTY": 
       if (e.SortDirection == SortDirection.Ascending) 
       { 
        var result = from table in Ob.DataTableOther.AsEnumerable() 
           orderby table.Field<int>("Qty") 
           select table; 
        var dv = result.AsDataView(); 
        grdReport.DataSource = dv; 
        grdReport.DataBind(); 
       } 
       else 
       { 
        var result = from table in Ob.DataTableOther.AsEnumerable() 
           orderby table.Field<int>("Qty") descending 
           select table; 
        var dv = result.AsDataView(); 
        grdReport.DataSource = dv; 
        grdReport.DataBind(); 
       }      
       break; 

      case "Volume": 
       if (e.SortDirection == SortDirection.Ascending) 
       { 
        var result = from table in Ob.DataTableOther.AsEnumerable() 
           orderby table.Field<float>("Volume") 
           select table; 
        var dv = result.AsDataView(); 
        grdReport.DataSource = dv; 
        grdReport.DataBind(); 
       } 
       else 
       { 
        var result = from table in Ob.DataTableOther.AsEnumerable() 
           orderby table.Field<float>("Volume") descending 
           select table; 
        var dv = result.AsDataView(); 
        grdReport.DataSource = dv; 
        grdReport.DataBind(); 
       } 
       break; 

     case "FirstBill": 
       if (e.SortDirection == SortDirection.Ascending) 
       { 
        var result = from table in Ob.DataTableOther.AsEnumerable() 
           orderby table.Field<DateTime>("FirstBill") 
           select table; 
        var dv = result.AsDataView(); 
        grdReport.DataSource = dv; 
        grdReport.DataBind(); 
       } 
       else 
       { 
        var result = from table in Ob.DataTableOther.AsEnumerable() 
           orderby table.Field<DateTime>("FirstBill") descending 
           select table; 
        var dv = result.AsDataView(); 
        grdReport.DataSource = dv; 
        grdReport.DataBind(); 
       } 
       break; 


      default: 
       break; 
     } 

和行數據綁定事件是

protected void grdReport_RowDataBound(object sender, GridViewRowEventArgs e) 
    { 
     # region try 
     try 
     { 
      if (e.Row.RowType == DataControlRowType.DataRow && Ob.DatasetMain.Tables[0].Rows.Count != 0) 
      { 
       if ((Ob.FromDate != null || Ob.FromDate != "") && (Ob.UptoDate != null || Ob.UptoDate != "")) 
       { 

        ((HyperLink)e.Row.Cells[2].FindControl("hplNavigate")).NavigateUrl = 
         String.Format("~//Reports/BookingByCustomerReport.aspx?BC={0},{1},{2},{3}", Ob.DatasetMain.Tables[0].Rows[Ob.Counter][0], Ob.FromDate, Ob.UptoDate, radReportFrom.Checked); 


        Ob.Counter++; 
       } 
       if (hdnFromCustomer.Value == "true") 
       { 
        ((CheckBox)e.Row.Cells[0].FindControl("chkSelect")).Checked = true; 
       } 
      } 

      if (e.Row.RowType == DataControlRowType.Footer) 
      { 
       if (Ob.DatasetOther.Tables[0].Rows.Count != 0) 
       { 
        e.Row.Cells[2].Text = "Total"; 
        e.Row.Cells[3].Text = Ob.DatasetOther.Tables[0].Rows[Ob.DatasetOther.Tables[0].Rows.Count - 1][2].ToString(); 
        e.Row.Cells[4].Text = Ob.DatasetOther.Tables[0].Rows[Ob.DatasetOther.Tables[0].Rows.Count - 1][3].ToString(); 
        e.Row.Cells[5].Text = Ob.DatasetOther.Tables[0].Rows[Ob.DatasetOther.Tables[0].Rows.Count - 1][4].ToString(); 
        e.Row.Cells[6].Text = Ob.DatasetOther.Tables[0].Rows[Ob.DatasetOther.Tables[0].Rows.Count - 1][5].ToString(); 
       } 
      } 
     } 
     # endregion 
     catch (Exception ex) 
     { } 
    } 

要再次列出問題

  1. 我不能排序DES當我再次點擊
  2. 如果我搜索結果排序方式說,客戶名稱,然後單擊qty或任何其他我得到Specified cast is not valid.手段,我無法通過一次單擊任何其他列理清我按任何特定的列排序。

任何人都可以幫我找出問題嗎?

回答

1

你必須讓你的GridView排序上你的GridView財產

AllowSorting="true" 

而且你有你在高速緩存中的排序(視圖狀態或會話)存儲。使用一個會話變量來存儲最新的排序表達式和時您下次對網格進行排序,將網格的排序表達式與存儲最後排序表達式的Session變量進行比較。如果列相同,則檢查上一個排序的方向並按相反方向排序。

實施例:

DataTable sourceTable = GridAttendence.DataSource as DataTable; 
DataView view = new DataView(sourceTable); 
string[] sortData = Session["sortExpression"].ToString().Trim().Split(' '); 
if (e.SortExpression == sortData[0]) 
{ 
    if (sortData[1] == "ASC") 
    { 
     view.Sort = e.SortExpression + " " + "DESC"; 
     this.ViewState["sortExpression"] = e.SortExpression + " " + "DESC"; 
    } 
    else 
    { 
     view.Sort = e.SortExpression + " " + "ASC"; 
     this.ViewState["sortExpression"] = e.SortExpression + " " + "ASC"; 
    } 
} 
else 
{ 
    view.Sort = e.SortExpression + " " + "ASC"; 
    this.ViewState["sortExpression"] = e.SortExpression + " " + "ASC"; 
} 
+0

你可能想改變'Session [「sortExpression」]'ViewState [「sortExpression」]' – naveen

1

在網格中時的AutoGenerateColumns =「假」時,e.SortDirection總是會上升。解決方法是,你需要存儲列的最後一個排序順序(或你每列的最後排序順序存儲)在ViewState中

public const string ASCENDING = "Ascending"; 
    public const string DESCENDING = "Descending"; 

定義屬性,保留了SortOrder的

public string GridSortOrder 
    { 
     get { return Convert.ToString(ViewState["SortOrderKey"]) == string.Empty ? ASCENDING : "Descending"; } 
     set { ViewState["SortOrderKey"] = value; } 
    } 

在grdReport_Sorting事件,取得了

 if (GridSortOrder == ASCENDING) 
     { 
      var result = from table in Ob.DataTableOther.AsEnumerable() 
         orderby table.Field<string>("CustomerName") 
         select table; 
      var dv = result.AsDataView(); 
      grdReport.DataSource = dv; 
      grdReport.DataBind(); 

     } 
     else 
     { 
      var result = from table in Ob.DataTableOther.AsEnumerable() 
         orderby table.Field<string>("CustomerName") descending 
         select table; 
      var dv = result.AsDataView(); 
      grdReport.DataSource = dv; 
      grdReport.DataBind(); 
     } 

     /* 
     * logic for remaining columns 
     */ 

     //Change the sortOrder 
     ChangeSortOrder(GridSortOrder); 

更改排序方法

public void ChangeSortOrder(string currentOrder) 
    { 
     GridSortOrder = currentOrder == ASCENDING ? DESCENDING : ASCENDING; 
    } 
0

試試這個辦法

private const string ASCENDING = " ASC"; 
private const string DESCENDING = " DESC"; 
static private DataView dvReports; 

protected void Page_Load(object sender, EventArgs e) 
{ 
    if (!IsPostBack) 
    { 
     // i am assuming that you are binding the gridview on page_load 
     // you can use the same on a button click event too 
     dvReports = new DataView(Ob.DataTableOther); 
     BindGridView(); 
    } 
} 

protected void grdReport_Sorting(object sender, GridViewSortEventArgs e) 
{ 
    string sortExpression = e.SortExpression; 

    if (GridViewSortDirection == SortDirection.Ascending) 
    { 
     GridViewSortDirection = SortDirection.Descending; 
     dvProducts.Sort = sortExpression + DESCENDING; 
    } 
    else 
    { 
     GridViewSortDirection = SortDirection.Ascending; 
     dvProducts.Sort = sortExpression + ASCENDING; 
    } 
    BindGridView(); 
} 

private void BindGridView() 
{ 
    GridView1.DataSource = dvReports 
    GridView1.DataBind(); 
} 

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

     return (SortDirection)ViewState["sortDirection"]; 
    } 
    set { ViewState["sortDirection"] = value; } 
} 

我寫這個給你,因爲如果你是一個DataTable結合沒有必要讓它IEnumerable,做自定義排序。這不是必需的,也有更多的代碼行。

框架是你的朋友。快樂編碼。