【发布时间】:2014-08-10 21:18:06
【问题描述】:
当用户单击列标题时,我想按列对网格视图进行排序。在这里,用户可以单击任何列,并且网格视图根据单击的列进行排序。这是我的代码:
<asp:GridView ID="gvEmployeeStatus" runat="server" AutoGenerateColumns="false" AllowPaging="True" PageSize="10" AllowSorting="true" OnPageIndexChanging="gvEmployeeStatus_PageIndexChanging" OnSorting="gvEmployeeStatus_Sorting" >
protected void gvEmployeeStatus_Sorting(object sender, GridViewSortEventArgs e)
{
loginName = (String)(Session["LoginName"]);
dsLoginDetail = clsBLogic.TblLogin(loginName);
tblEmployeeNo = dsLoginDetail.Tables[0].Rows[0]["EmployeeNo"].ToString();
BindDataTogvEmployeeStatus(tblEmployeeNo);
DataTable dataTable = gvEmployeeStatus.DataSource as DataTable;
if(dataTable != null)
{
DataView dataView = new DataView(dataTable);
dataView.Sort = e.SortExpression + " " + ConvertSortDirection(e.SortDirection);
gvEmployeeStatus.DataSource = dataView;
gvEmployeeStatus.DataBind();
}
}
private string ConvertSortDirection(SortDirection sortDirection)
{
string newSortDirection = String.Empty;
switch (sortDirection)
{
case SortDirection.Ascending:
newSortDirection = "ASC";
break;
case SortDirection.Descending:
newSortDirection = "DESC";
break;
}
return newSortDirection;
}
网格视图中的记录来自数据集。现在,当我运行代码时,什么也没有发生。我在 gvEmployeeStatus_Sorting 事件上设置了断点来检查它何时会被触发。它没有被解雇。我怎样才能对记录进行排序!
【问题讨论】: