通过单击标题文本排序GridView列时出错

时间:2014-07-03 08:18:47

标签: c# asp.net

我从数据集中绑定了asp.net中的GridView。这里我按标题文本排序网格视图。我的代码是:

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

      protected void gridLeaveRequest_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)
        {
            dsLoginDetail = clsBLogic.GetLoginDetailByEmployeeNo(Convert.ToString(Session["LoginName"]));
            emoloyeeNo = dsLoginDetail.Tables[0].Rows[0]["EmployeeNo"].ToString();
            DataSet dsLeaveRequest = new DataSet();        
            dsLeaveRequest = clsBLogic.GetLeaveRequestByEmployeeNo(emoloyeeNo);        
            DataTable dt = dsLeaveRequest.Tables[0];
            DataView dv = new DataView(dt);
            dv.Sort = sortExpression + direction;
            gridLeaveRequest.DataSource = dv;
            gridLeaveRequest.DataBind();
        } 


 <asp:GridView ID="gridLeaveRequest" EmptyDataText="No Record Found..."  runat="server" AutoGenerateColumns="false"
                AllowPaging="True" PageSize="20" OnPageIndexChanging="gridLeaveRequest_PageIndexChanging" AllowSorting="True" OnSorting="gridLeaveRequest_Sorting">

<asp:BoundField DataField="Purpose" HeaderText="Purpose" ItemStyle-Width="40%" SortExpression="Purpose" >
</asp:BoundField>

当我点击gridview的目标标题时,我在"Cannot find column PurposeDESCENDING."上遇到运行时错误dv.sort=sortExpression + direction;     任何帮助?提前谢谢

1 个答案:

答案 0 :(得分:0)

使用DESCENDING

更改DESC

使用ASCENDING

更改ASC

SortExpressionSortDirection;

之间添加空格
 dv.Sort = sortExpression + " " + direction;