动态数据中的字段顺序?

发布于 2024-07-08 20:18:57 字数 77 浏览 3 评论 0原文

有谁知道是否可以在动态数据中选择字段的顺序(当然,无需自定义每个表的模板)?

谢谢 !

Does anybody know if it is possible to choose the order of the fields in Dynamic Data (of course, without customizing the templates of each table) ?

Thanks !

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

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

发布评论

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

评论(7

屋檐 2024-07-15 20:18:57

在.NET 4.0中,使用4.0版本的动态数据dll,您可以像这样设置数据注释:

[Display(Name = " Mission Statement", Order = 30)]
public object MissionStatement { get; set; }

[Display(Name = "Last Mod", Order = 40)]  
public object DateModified { get; private set; }

In .NET 4.0, using the 4.0 release of the Dynamic Data dll, you can set data annotations like so:

[Display(Name = " Mission Statement", Order = 30)]
public object MissionStatement { get; set; }

[Display(Name = "Last Mod", Order = 40)]  
public object DateModified { get; private set; }
臻嫒无言 2024-07-15 20:18:57

根据 this 线程 - 您可以在动态数据 futures dll 中使用 ColumnOrderAttribute 。 您可以从 Codeplex 获取期货。

As per this thread - you can use the ColumnOrderAttribute in the dynamic data futures dll. You can grab the futures from codeplex.

自由如风 2024-07-15 20:18:57

您可以通过修改 LINQ to SQL 文件中公共属性的顺序来完成此操作。

例如,我进入 Northwind.designer.cs(这是我自动生成的 LINQ to SQL 文件),并将名为 Products 的公共属性移至公共分部类 Category 中的公共属性 CategoryName 之上。 然后我重新编译,默认模板以我的新顺序显示列。

当然,这意味着您编辑自动生成的代码,如果重新生成它,您的更改就会丢失,因此这种技术并非没有危险。

You can do this by modifying the order of the public properties in your LINQ to SQL file.

For example, I went into Northwind.designer.cs which was my auto-generated LINQ to SQL file and moved the public property named Products above the public property CategoryName in the public partial class Category. Then I recompiled and the default template displayed the columns in my new order.

Of course, this means your editing auto-generated code and if you regenerate it, your changes are lost, so this technique is not without peril.

千秋岁 2024-07-15 20:18:57

您必须在 DynamicData 文件夹中创建自定义页面。

步骤如下:

  • 在“DynamicData\CustomPages”文件夹下创建一个与要自定义列顺序的表名称同名的文件夹 在
  • “DynamicData\CustomPages\[具有表名称的文件夹]”下创建自定义页面文件夹。
    • 我只是将现有的“List.aspx”文件从“DynamicData\PageTemplates”复制到上面的文件夹中。
  • 打开 aspx 文件并将 GridView 控件修改为“AutoGenerateColumns='false'”
  • 在 GridView 的列部分中,按照所需的顺序将具有“DataField”属性值的“DynamicControl”控件添加到列名称中。

以下是 ScottHa 的截屏视频:
http://www.asp.net/learn/3.5-SP1/ video-293.aspx

You have to create a custom page in DynamicData folder.

Here are the steps:

  • Create a folder that is the same name as your table name that you want to customize the ordering of columns under "DynamicData\CustomPages" folder
  • Create a custom page under "DynamicData\CustomPages\[folder with table name]" folder.
    • I just copy the existing "List.aspx" file from "DynamicData\PageTemplates" into the folder above.
  • Open the aspx file and modify GridView control to "AutoGenerateColumns='false'"
  • Inside columns section of GridView, add "DynamicControl" controls with the "DataField" attribute value to the name of your column in the order you want.

Here is a screencast from ScottHa:
http://www.asp.net/learn/3.5-SP1/video-293.aspx

◇流星雨 2024-07-15 20:18:57

GridView 具有 ColumnsGenerator 属性,通过实现 IAutoFieldGenerator 接口的GenerateFields 方法来使用它,您可以在其中根据自定义规则(属性、元信息等)设置字段顺序

protected override void OnInit(EventArgs e)
{
    ...
    this.gvItemsList.ColumnsGenerator = new EntityFieldsGenerator(CurrentDataSource.CurrentTableMetadata);
    ...
}

public class EntityFieldsGenerator : IAutoFieldGenerator {
...
public ICollection GenerateFields(Control control)    
{
    // based on entity meta info
    var fields = from item in this.entityMetadata.Columns
                 where this.IncludeColumn(item.Value)
                 orderby item.Value.Order
                 select new DynamicField
                 {
                     DataField = item.Value.Column.Name,
                     HeaderText = item.Value.DisplayName,
                     DataFormatString = item.Value.DataFormatString,
                     UIHint = GetColumnUIHint(item.Value)
                 };
    return fields.ToList();
} }

GridView have ColumnsGenerator property, use it by implementing GenerateFields method of IAutoFieldGenerator interface in which you can set fields orders based on your custom rules (attributes, meta info, ...)

protected override void OnInit(EventArgs e)
{
    ...
    this.gvItemsList.ColumnsGenerator = new EntityFieldsGenerator(CurrentDataSource.CurrentTableMetadata);
    ...
}

public class EntityFieldsGenerator : IAutoFieldGenerator {
...
public ICollection GenerateFields(Control control)    
{
    // based on entity meta info
    var fields = from item in this.entityMetadata.Columns
                 where this.IncludeColumn(item.Value)
                 orderby item.Value.Order
                 select new DynamicField
                 {
                     DataField = item.Value.Column.Name,
                     HeaderText = item.Value.DisplayName,
                     DataFormatString = item.Value.DataFormatString,
                     UIHint = GetColumnUIHint(item.Value)
                 };
    return fields.ToList();
} }
那一片橙海, 2024-07-15 20:18:57

为了避免使用动态数据 futures dll,您可以按如下方式滚动自己的 ColumnOrder 属性:

[AttributeUsage(AttributeTargets.Property)]
public class ColumnOrderAttribute : Attribute
{
    public int Order { get; private set; }
    public ColumnOrderAttribute() { Order = int.MaxValue; }
    public ColumnOrderAttribute(int order) { Order = order; }
    public static ColumnOrderAttribute Default = new ColumnOrderAttribute();
}

然后在实现 IAutoFieldGenerator 的类中,您

public static class ExtensionMethods
{
    public static int GetOrder (this MetaColumn column)
    {
        var orderAttribute = column.Attributes.OfType<ColumnOrderAttribute>().DefaultIfEmpty(ColumnOrderAttribute.Default).Single();
        return orderAttribute.Order;
    }
}

public ICollection GenerateFields(Control control)
{
    var fields = new List<DynamicField>();
    var columns = _table.Columns.OrderBy(column => column.GetOrder());
    foreach (var column in columns)
    {
        if (!column.Scaffold) { continue; }
        fields.Add(new DynamicField {DataField = column.Name});
    }
}

最终的用法如下

[MetadataType(typeof(CustomerMetadata))]
public partial class Customer {}

public class CustomerMetadata
{
    [ColumnOrder(1)]
    public object FirstName {get;set;}
    [ColumnOrder(2)]
    public object LastName {get;set;}
}

To avoid using the Dynamic Data futures dll, you can roll your own ColumnOrder attribute as follows:

[AttributeUsage(AttributeTargets.Property)]
public class ColumnOrderAttribute : Attribute
{
    public int Order { get; private set; }
    public ColumnOrderAttribute() { Order = int.MaxValue; }
    public ColumnOrderAttribute(int order) { Order = order; }
    public static ColumnOrderAttribute Default = new ColumnOrderAttribute();
}

and then in your class that implements IAutoFieldGenerator, you have

public static class ExtensionMethods
{
    public static int GetOrder (this MetaColumn column)
    {
        var orderAttribute = column.Attributes.OfType<ColumnOrderAttribute>().DefaultIfEmpty(ColumnOrderAttribute.Default).Single();
        return orderAttribute.Order;
    }
}

public ICollection GenerateFields(Control control)
{
    var fields = new List<DynamicField>();
    var columns = _table.Columns.OrderBy(column => column.GetOrder());
    foreach (var column in columns)
    {
        if (!column.Scaffold) { continue; }
        fields.Add(new DynamicField {DataField = column.Name});
    }
}

and finally your usage would look like

[MetadataType(typeof(CustomerMetadata))]
public partial class Customer {}

public class CustomerMetadata
{
    [ColumnOrder(1)]
    public object FirstName {get;set;}
    [ColumnOrder(2)]
    public object LastName {get;set;}
}
兲鉂ぱ嘚淚 2024-07-15 20:18:57

我正在回答一个老问题,因为在我看来,可能的解决方案在新版本的框架中发生了变化。

看来 Display(Order) 现在可以直接按照要求工作(.NET 4.0 上的 Visual Web Developer 2010),无需任何特定的解决方法。

示例:

[Display(Order = 50)]

一件重要的事情是检查正确的对象名称以映射外键:

在一个项目中,实体类中的字段 OperatoreID 被翻译为:

public object Operatore { get; set; }

作为 Operatore 外键的源表; 对于同一张表上的第二个引用,它将得到类似 1 的值,依此类推。

I'm answering an old question because it seems to me that the possible solution changed in newer versions of the framework.

It seems that the Display(Order) works now directly as asked (Visual Web Developer 2010 on .NET 4.0) without any particular workaround.

Example:

[Display(Order = 50)]

An important thing it's to check the correct object name to map the foreignkey:

in one project a field OperatoreID translated in the entity class as:

public object Operatore { get; set; }

being Operatore the source table of the foreignkey; for a second reference on the same table it will get something like 1 and so on.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文