Asp.net:使用 gridview 和 objectDataSource 进行排序

发布于 2024-10-15 17:00:28 字数 51 浏览 1 评论 0原文

如何使用 ObjectDataSource 绑定的数据在 gridView 中进行排序?

how I do a sorting in a gridView with data bound by a ObjectDataSource?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

少女情怀诗 2024-10-22 17:00:28

这里是一个之前已经回答过的问题。

对于实际的排序,您可以调用

collectionOfObjects.OrderBy(x => x.PropertyToSortOn);

您可以使用开关根据通过参数传递到方法中的内容来更改排序依据。所以它看起来更像这样

switch(propertyName)
{
  case "property1":
    collectionOfObjects.OrderBy(x => x.PropertyToSortOn);
    break;
  case "property2":
    collectionOfObjects.OrderBy(x => x.OtherPropertyToSortOn);
    break;

  ...

}

希望这有帮助! :)

Here is a question that has been previously answered.

For the actual sorting, you would call

collectionOfObjects.OrderBy(x => x.PropertyToSortOn);

You could use a switch to change what to sort on based on what is passed into the method via the args. So it would look a little more like this

switch(propertyName)
{
  case "property1":
    collectionOfObjects.OrderBy(x => x.PropertyToSortOn);
    break;
  case "property2":
    collectionOfObjects.OrderBy(x => x.OtherPropertyToSortOn);
    break;

  ...

}

Hope this helps! :)

岁吢 2024-10-22 17:00:28

如果对您来说更容易,为什么不尝试从存储过程或查询中对其进行排序。
也许不是最佳解决方案,但可能更容易。

编辑

如果您想使用 gridview 控件以编程方式执行此操作,请查看以下代码:

protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GridView1.PageIndex = e.NewPageIndex;
            GridView1.DataBind();
        }


        protected void gridView_Sorting(object sender, GridViewSortEventArgs e)
        {
            DataTable dtSortTable = GridView1.DataSource as DataTable;
            if (dtSortTable != null)
            {
                DataView dvSortedView = new DataView(dtSortTable);
                dvSortedView.Sort = e.SortExpression + " " + getSortDirectionString(e.SortDirection);
                GridView1.DataSource = dvSortedView;
                GridView1.DataBind();
            }
        }

        private string getSortDirectionString(SortDirection sortDireciton)
        {
             string newSortDirection = String.Empty;
             if (sortDireciton == SortDirection.Ascending)
            {
                newSortDirection = "ASC";
            }
            else
            {
                newSortDirection = "DESC";
            }
            return newSortDirection;
        }

If is more easy for you, why dont you try to sort it from the store procedure or the query.
Maybe is not the optimun solution but it could be easier.

EDIT

IF you want to do it programattically with the gridview controls, Take a look at this code:

protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GridView1.PageIndex = e.NewPageIndex;
            GridView1.DataBind();
        }


        protected void gridView_Sorting(object sender, GridViewSortEventArgs e)
        {
            DataTable dtSortTable = GridView1.DataSource as DataTable;
            if (dtSortTable != null)
            {
                DataView dvSortedView = new DataView(dtSortTable);
                dvSortedView.Sort = e.SortExpression + " " + getSortDirectionString(e.SortDirection);
                GridView1.DataSource = dvSortedView;
                GridView1.DataBind();
            }
        }

        private string getSortDirectionString(SortDirection sortDireciton)
        {
             string newSortDirection = String.Empty;
             if (sortDireciton == SortDirection.Ascending)
            {
                newSortDirection = "ASC";
            }
            else
            {
                newSortDirection = "DESC";
            }
            return newSortDirection;
        }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文