首页 > 编程语言 >asp GridView AllowSorting 不生效

asp GridView AllowSorting 不生效

时间:2022-10-29 12:37:06浏览次数:50  
标签:GridViewSortDirection GridView sortExpression Id SortDirection asp AllowSorting 


最近在维护一个N年老项目,想着在Gridview 设置 AllowSorting="true" 排序还不管用。

 

<asp:GridView ID="GridView1" runat="server" AllowPaging="True" 
AutoGenerateColumns="False"
CellPadding="4" CssClass ="gridview"
onpageindexchanging="GridView1_PageIndexChanging" PageSize="20"
EnableSortingAndPagingCallbacks ="false" Width="97%"
AllowSorting="true"
OnSorting="GvList_Sorting"
>

        后来,发现必须指定要排序的列才行的。 这样的列头下面鼠标移动上去就会出现一个下滑列。

asp GridView AllowSorting 不生效_升序

asp GridView AllowSorting 不生效_allowsort不起作用_02

 下面分享下排序的代码:

protected void GvList_Sorting(object sender, GridViewSortEventArgs e)
{
Console.WriteLine("单击排序。。。");


string sortExpression = e.SortExpression;

if (GridViewSortDirection == SortDirection.Ascending)

{

GridViewSortDirection = SortDirection.Descending;

SortGridView(sortExpression, "desc");

}

else

{

GridViewSortDirection = SortDirection.Ascending;

SortGridView(sortExpression, "asc");

}

}
public SortDirection GridViewSortDirection
{

get

{

if (ViewState["sortDirection"] == null)

ViewState["sortDirection"] = SortDirection.Ascending;

return (SortDirection)ViewState["sortDirection"];

}

set { ViewState["sortDirection"] = value; }

}

private void SortGridView(string sortExpression, string direction)
{
DataTable dataTable = ((DataSet)GridView1.DataSource).Tables[0];
//dataTable.DefaultView.Sort = "Id DESC";//按Id倒序
//dataTable.DefaultView.Sort = "Id DESC,Name desc";//按Id倒序和Name倒序

dataTable.DefaultView.Sort = sortExpression+" "+ direction;//按Id倒序
dataTable = dataTable.DefaultView.ToTable();
GridView1.DataSource = dataTable;
GridView1.DataBind();
}

    至此,点击列头的文字,即可排序了。升序和倒叙,都可以。

标签:GridViewSortDirection,GridView,sortExpression,Id,SortDirection,asp,AllowSorting,
From: https://blog.51cto.com/51souta/5806017

相关文章