在GridView中排序和分页 [英] Sort and Paging in GridView

查看:94
本文介绍了在GridView中排序和分页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只想对显示的页面(例如pg 2)而不是整个数据源进行网格视图排序,分页效果很好,但排序却没有.尝试在pageindexchanging上执行此操作时也遇到错误:

I want to sort my grid view on only for the page it is showing(example pg 2) and not the whole datasource.Paging works fine but sorting doesnt.I also got an error while attempting to do this on pageindexchanging:

GridView2.PageIndex = e.NewPageIndex;
            GridView2.DataSource = DataBase1.myProcedureName(CollegeID, Dept_ID, category, Loc_ID);

            DataTable dataTable = GridView2.DataSource as DataTable;

            if (dataTable != null)
            {
                DataView dataView = new DataView(dataTable);
                //dataView.Sort = GetSortExpression(e.SortExpression);
                GridView2.DataSource = dataView;
                GridView2.DataBind();

            }
            GridView2.DataBind();



还有另一种方法吗?

使用以下命令对Im进行排序:



Is there another way to do it.

For sorting Im using this:

protected void Gridview2_Sorting(object sender, GridViewSortEventArgs e)
   {
GridView2.DataSource = DataBase1.myProcedureName(CollegeID, Dept_ID, category, Loc_ID);

        DataTable dataTable = GridView2.DataSource as DataTable;

        if (dataTable != null)
        {
            DataView dataView = new DataView(dataTable);
            dataView.Sort = GetSortExpression(e.SortExpression);
            GridView2.DataSource = dataView;
            GridView2.DataBind();

        }
    }

private string GridViewSortDirection
   {
       get { return ViewState["SortDirection"] as string ?? "ASC"; }
       set { ViewState["SortDirection"] = value; }
   }
   private string GridViewSortExpression
   {
       get { return ViewState["SortExpression"] as string ?? string.Empty; }
       set { ViewState["SortExpression"] = value; }
   }

   private string GetSortExpression(string sortExpression)
   {
       if (sortExpression == GridViewSortExpression && GridViewSortDirection == "ASC")
       {
           GridViewSortDirection = "DESC";
       }
       else
       {
           GridViewSortDirection = "ASC";
       }
       GridViewSortExpression = sortExpression;
       return sortExpression + " " + GridViewSortDirection;
   }

推荐答案

看看关于CP的文章:
GridView分页和排序 [ GridView:排序和使用C#GetValueOrDefault()的泛型和可空数据类型函数进行分页 [ ^ ]

您可以尝试在 CodeProject [ ^ ]或 CP答案 [ ^ ]
Have a look on articles on CP:
GridView Paging and Sorting[^]
GridView:Sorting & Paging Using Generics And Nullable Datatype Function Of C# GetValueOrDefault()[^]

You can try search on CodeProject[^] or google[^]

More similar QA I have answered before: CP Answers[^]


U可以在当前分页中引用以下链接进行查找.

http://c-sharp-corner.blogspot. in/2011/08/sorting-only-current-page-of-gridview.html [
U can refer the below link for soting with in the current paging .

http://c-sharp-corner.blogspot.in/2011/08/sorting-only-current-page-of-gridview.html[^]


这篇关于在GridView中排序和分页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆