如何在单击列标题时对网格视图中的记录进行排序
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何在单击列标题时对网格视图中的记录进行排序相关的知识,希望对你有一定的参考价值。
我希望在用户单击列标题时按列对网格视图进行排序。在这里,用户可以单击任何列,并根据单击的列对网格视图进行排序。这是我的代码:
<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事件上设置了断点,以检查它何时被触发。它没有被解雇。我该怎么排序记录!!
答案
正如你提到的排序没有被触发,我想你错过了SortExpression
中的属性<asp:TemplateField> or <asp:BoundField>
例如,
<asp:TemplateField HeaderText="Your Header 1" SortExpression="ColumName1">
要么
<asp:BoundField DataField="Your Header 1" HeaderText="Your Header 1" SortExpression="ColumName1" />
另一答案
在aspx页面上将此属性设置为gridview。
onsortcommand="ComponentGridView_Sorting
。
然后将它放在aspx.cs页面的后端。
protected void Datagrid1_SortCommand(object source, DataGridSortCommandEventArgs e)
{
string strSQL;
DataTable dt;
strSQL = "(SQL SELECT STATEMENT HERE)";
dt = strSQL;
{
string SortDir = string.Empty;
if (dir == SortDirection.Ascending)
{
dir = SortDirection.Descending;
SortDir = "Desc";
}
else
{
dir = SortDirection.Ascending;
SortDir = "Asc";
}
DataView sortedView = new DataView(dt);
sortedView.Sort = e.SortExpression + " " + SortDir;
Datagrid1.DataSource = sortedView;
Datagrid1.DataBind();
}
}
protected SortDirection dir
{
get
{
if (ViewState["dirState"] == null)
{
ViewState["dirState"] = SortDirection.Ascending;
}
return (SortDirection)ViewState["dirState"];
}
set
{
ViewState["dirState"] = value;
}
}
另一答案
#region For Grid view Header Sorting..!!
public SortDirection dir
{
get
{
if (ViewState["dirState"] == null)
{
ViewState["dirState"] = SortDirection.Ascending;
}
return (SortDirection)ViewState["dirState"];
}
set
{
ViewState["dirState"] = value;
}
}
protected void grdAdd_Sorting(object sender, GridViewSortEventArgs e)
{
string sortingDirection = string.Empty;
if (dir == SortDirection.Ascending)
{
dir = SortDirection.Descending;
sortingDirection = "Desc";
}
else
{
dir = SortDirection.Ascending;
sortingDirection = "Asc";
}
DataTable dtgrd = AdditionBL.BindAdditionMaster();/**Data Table Bind For Short View**/
DataView sortedView = new DataView(dtgrd);
sortedView.Sort = e.SortExpression + " " + sortingDirection;
grdAddition.DataSource = sortedView;
grdAddition.DataBind();
}
#endregion
以上是关于如何在单击列标题时对网格视图中的记录进行排序的主要内容,如果未能解决你的问题,请参考以下文章