我从数据集中绑定了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;
任何帮助?提前谢谢
答案 0 :(得分:0)
使用DESCENDING
DESC
使用ASCENDING
ASC
在SortExpression
和SortDirection
;
dv.Sort = sortExpression + " " + direction;