单击标题文本对GridView列进行排序时出错
本文关键字:排序 出错 标题 文本 GridView 单击 | 更新日期: 2023-09-27 18:11:11
我正在从数据集绑定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的目的标题时,我在dv.sort=sortExpression + direction;
上得到运行时错误"Cannot find column PurposeDESCENDING."
任何帮助吗?提前感谢
用DESC
替换DESCENDING
用ASC
替换ASCENDING
在SortExpression
和SortDirection
之间添加空格;
dv.Sort = sortExpression + " " + direction;