views:

2311

answers:

8

Hi,

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 !

+1  A: 

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.

AndrewDotHay
A: 

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

Joey V.
+2  A: 

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

TheDeeno
A: 

Hi,

For some of my Dynamic Data meta data tables I would like to control the order of the displayed columns. I have a custom page and I created a sub-directory named the same as my table. I copied the ListDetails.aspx and code file to the new directory. And changed AutoGenerateColumns to false(in Gridview) and AutoGenerateRows to false(DetailsView. None of them worked! Please please help me. J

Best regards,

Sahar

            <Columns>
              <asp:DynamicField  DataField="DestFieldTypeDescription" />
              <asp:DynamicField  DataField="DestFieldTypeName" /> 
              <asp:DynamicField  DataField="DestFieldTypeID" />




            </Columns>
            <PagerStyle CssClass="footer" />        
            <SelectedRowStyle CssClass="selected" />
            <PagerTemplate>
                <asp:GridViewPager runat="server" />
            </PagerTemplate>
            <EmptyDataTemplate>
                There are currently no items in this table.
            </EmptyDataTemplate>
        </asp:GridView>

You are asking a lot, actually way too much, by simply saying that something "didn't work". Was it on strike?
ProfK
This is a question, not an answer. Ask questions where it says "Ask Question" top-right of the page.
Meff
+1  A: 

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();
} }
A: 

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;}
}
Brian Hinchey
A: 

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; }
Ash Machine
A: 

[Display(Name = "Last Mod", Order = 40)]

this is nice, but it doesn't work on the Foreign Key. My foreign keys are all located at the last columns, is it possible to bring it to the front or middle?

Thanks. Please advise.

Chris
If you have a follow-up question you should better ask it as a new question, not post it as an answer. More people would see it that way and would try to answer.
sth