在 Asp.Net 动态数据应用程序中使用 DisplayColumnAttribute 按多个字段排序?

发布于 2024-08-11 19:56:34 字数 154 浏览 6 评论 0原文

我有一个动态数据应用程序,我想根据多个字段进行排序,如下所示..

<DisplayColumn("FieldName", "Field1, Field2")> _

DisplayColumn 似乎不支持多个字段?

I've got a dynamic data app, and I want to sort based on multiple fields like so..

<DisplayColumn("FieldName", "Field1, Field2")> _

DisplayColumn doesn't seem to support more than one field?

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

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

发布评论

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

评论(3

人│生佛魔见 2024-08-18 19:56:34

DisplayColumn 属性的 SortColumn 参数指定当该实体用作外键时(例如:在 DropDownList 中)而不是在 GridView 中排序(或在 DB 中排序)时用于对此实体进行排序的列。

也可以在这里看到:
ASP.NET 动态数据 DisplayColumn 属性导致排序问题

msdn: http://msdn.microsoft.com/ en-us/library/system.componentmodel.dataannotations.displaycolumnattribute.aspx

如果这仍然是您正在寻找的内容(仅在 DropDownLists 中排序,而不是在数据库中排序),您可以通过在中创建自定义属性来对多列进行排序组合这两列的实体。

例子:

[DisplayColumn("FieldName", "SortColumn")]
public partial class Employee
{
    public string SortColumn
    {
        get { return Field1 + Field2; }
    }
}

The DisplayColumn attribute's SortColumn param specifies the column that will be used to sort this entity when it is used as a foreign key (ex: in a DropDownList), not when it is being sorted in the GridView (or being sorted in the DB).

see SO here as well:
ASP.NET Dynamic Data DisplayColumn Attribute Causing Sorting Issue

msdn: http://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations.displaycolumnattribute.aspx

If this is still what you are looking for (sorting in DropDownLists only, not the DB) you can sort on multiple columns by creating a custom property in the entity that combines those two columns.

example:

[DisplayColumn("FieldName", "SortColumn")]
public partial class Employee
{
    public string SortColumn
    {
        get { return Field1 + Field2; }
    }
}
π浅易 2024-08-18 19:56:34

如果您想对多列上的 gridview 进行排序,那么您可以这样做 -

在 Page Template\List.aspx.cs & 中在 Page_Laod 事件中检查表名

if (!Page.IsPostBack)
{
if (表.Name == "项目")
{

GridView1.Sort("ClientDetail.CompanyName asc,ProjectName asc", SortDirection.Ascending);

}

还可以为每列提供升序或降序的顺序。

If you want to sort gridview on multiple columns then this is how you can do -

In Page Template\List.aspx.cs & in Page_Laod event check for table name

if (!Page.IsPostBack)
{
if (table.Name == "Project")
{

GridView1.Sort("ClientDetail.CompanyName asc,ProjectName asc", SortDirection.Ascending);

}

}

and you can also provide order either ascending or descending for each column.

荆棘i 2024-08-18 19:56:34

是的,它确实支持多个字段。您可以尝试以下操作:

[DisplayColumn("FieldName", "Field1 ASC, Field2 DESC, Field3")]

然后在 List.aspxPage_Load 中:

var displayColumn = table.GetAttribute<DisplayColumnAttribute>();
if (displayColumn != null && displayColumn.SortColumn != null)
{
    GridView1.Sort(displayColumn.SortColumn,
    displayColumn.SortDescending ? SortDirection.Descending : SortDirection.Ascending);
}

Yes it does support multiple field. You can try this:

[DisplayColumn("FieldName", "Field1 ASC, Field2 DESC, Field3")]

Then in Page_Load of List.aspx:

var displayColumn = table.GetAttribute<DisplayColumnAttribute>();
if (displayColumn != null && displayColumn.SortColumn != null)
{
    GridView1.Sort(displayColumn.SortColumn,
    displayColumn.SortDescending ? SortDirection.Descending : SortDirection.Ascending);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文