本文介绍了如何排序的GridView,当我在asp.net点击gridview的标题文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经开发出具有GridView控件的asp.net应用程序。我希望它进行排序,当我点击在asp.net GridView的标题文本。实体框架用于GridView控件绑定。

 保护无效grdmortgagesaver_Sorting(对象发件人,GridViewSortEventArgs E)
{
    字符串SORTEX pression = e.SortEx pression;    如果(GridViewSortDirection == SortDirection.Ascending)
    {
        GridViewSortDirection = SortDirection.Descending;
        SortGridView(SORTEX pression,降);
    }
    其他
    {
        GridViewSortDirection = SortDirection.Ascending;
        SortGridView(SORTEX pression,递增);
    }}私人无效SortGridView(字符串SORTEX pression,串方向)
{ //你可以缓存DataTable中提高性能
     在mortgageentites.Clients VAR数据绑定=从我的OrderBy i.LastName选择我;
    如果(databind.Count()大于0)
    {
        grdmortgagesaver.DataSource = databind.ToList();
        grdmortgagesaver.DataBind();
        DataTable的DT =客户;
        如果(DT!= NULL)
        {
            // DataTable的DT = ds.Tables [0];
            DataView的DV =新的数据视图(DT);
            dv.Sort = SORTEX pression +方向;
            grdmortgagesaver.DataSource = DV;
            grdmortgagesaver.DataBind();
        }
    }
}


解决方案

如果您使用的是标准的ASP.NET System.Web.UI.WebControls.GridView,你可以简单地启用排序,在属性设置窗格AllowSorting为true或通过弹出式在控制右上方勾选启用排序

I have developed an asp.net application that has a Gridview control. I want it to sort when I click on the Gridview header text in asp.net. Entity framework is used to bind the gridview.

protected void grdmortgagesaver_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)
{

 //  You can cache the DataTable for improving performance
     var databind = from i in mortgageentites.Clients orderby i.LastName select i;
    if (databind.Count() > 0)
    {
        grdmortgagesaver.DataSource = databind.ToList();
        grdmortgagesaver.DataBind();
        DataTable dt = clients;
        if (dt != null)
        {
            // DataTable dt = ds.Tables[0];
            DataView dv = new DataView(dt);
            dv.Sort = sortExpression + direction;
            grdmortgagesaver.DataSource = dv;
            grdmortgagesaver.DataBind();
        }
    }
}
解决方案

If you are using a standard ASP.NET System.Web.UI.WebControls.GridView, you can simply "Enable Sorting", in the properties pane by setting "AllowSorting" to true or via the "pop-out" on the top right of the control by checking "Enable Sorting"

这篇关于如何排序的GridView,当我在asp.net点击gridview的标题文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-28 22:13