【问题标题】:Dynamically sorting the asp.net gridview动态排序asp.net gridview
【发布时间】:2015-03-03 06:47:28
【问题描述】:

我在asp.net c# 中的应用程序中有一个Gridview。现在我想动态地将排序属性添加到网格视图中。我不想将 AllowSorting="true" 放在 html 页面中。 Gridview 将如此简单。但是排序将由c#控制。有可能吗?我搜索了很长时间,但没有找到任何解决方案。请尽快帮助我。

【问题讨论】:

  • 亲爱的 Frebin, 请注意这个例子不是我要找的东西。 AllowSorting="true" 用于 asp.net html 页面。我只想采用简单的 girdview 并通过 C# 处理排序。
  • 您所说的“我只想采用简单的 girdview”是什么意思?你能说得更具体些吗?
  • 现在我想通过 c# 添加排序属性,而不是在 html 页面中使用 AllowSorting="true"

标签: c# asp.net sorting gridview


【解决方案1】:

您应该能够将 GridView 绑定到任何 IEnumerable 对象。然后使用 LINQ 对这些项目进行排序。

class DataRecord
{
    public Guid ID { get; set; }
    public String Name { get; set; }
    public DateTime CreateDate { get; set; }
};

List<DataRecord> data = new List<DataRecord>()
{
    new DataRecord()
    {
        ID = Guid.NewGuid(),
        Name = "Record 1",
        CreateDate = DateTime.Now
    },
    new DataRecord()
    {
        ID = Guid.NewGuid(),
        Name = "Record 2",
        CreateDate = DateTime.Now.AddDays(-1)
    }
};

gridView.DataSource = data.OrderBy(x => x.CreateDate);
gridView.DataBind();

【讨论】:

    猜你喜欢
    • 2017-01-07
    • 2023-03-12
    • 1970-01-01
    • 2016-07-31
    • 2017-03-28
    • 2011-09-02
    • 2011-01-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多