Dynamic Data Entity Framework: Initial Sort Order

Last post 01-20-2009 4:34 AM by sjnaughton. 9 replies.

Sort Posts:

  • Dynamic Data Entity Framework: Initial Sort Order

    01-18-2009, 9:12 PM
    • Member
      1 point Member
    • BHunsaker
    • Member since 01-19-2009, 1:54 AM
    • Posts 9

    Dynamic Data 3.5 SP1

    For just one of my Dynamic Data Entities tables I would like to control the order of the displayed records, specifically I would like to order a date column descending.

    The (SQL Server) table is named "EventLog" with a column "datLocalTimestamp" of type DateTime.

    In my "meta" .cs file, I have tried adding the following:

     
    [MetadataType(typeof(EventLogMetaData)), DisplayColumn("datLocalTimestamp", "datLocalTimestamp", true)]

    public partial class EventLog

    {

    }

     

    This did nothing.  Next, I tried adding a IComparer class:

     
    internal class EventLogCompare : IComparer<EventLog>

    {

    public int Compare( EventLog logA, EventLog logB )

    {

    return DateTime.Compare( logB.datLocalTimestamp, logA.datLocalTimestamp);

    }

    }


    I put a breakpoint on the Compare method but it is never invoked.

    Do I need to modify the Page_Load event in the ListDetails.aspx.cs page template file?  That doesn't seem like the way to go for a single table...

    Thanks for your help.

  • Re: Dynamic Data Entity Framework: Initial Sort Order

    01-19-2009, 3:25 AM
    • Star
      12,288 point Star
    • sjnaughton
    • Member since 04-29-2008, 1:11 PM
    • Newton-le-Willows, Merseyside, UK
    • Posts 2,562
    • TrustedFriends-MVPs

    I don't think that the Compare method would ever be called as your are either useing Linq to SQL or Entity Framework so the order by statement would be sent to the DB the thing to do (if you are using SQL Server) would be to run an SQL trace in the background whilst runnung your app.

    You'll then see if the order by is sent to the db.

    Steve Big Smile

    Always seeking an elegant solution.
    [Oh! If olny I colud tpye!]
    c# Bits blog
    Oh, and don't forget to mark as answer any posts that help you Big Smile
  • Re: Dynamic Data Entity Framework: Initial Sort Order

    01-19-2009, 5:25 AM
    • Member
      1 point Member
    • BHunsaker
    • Member since 01-19-2009, 1:54 AM
    • Posts 9

     Whether or not the DisplayColumn attribute is specified, a trace shows that the query has only the primary key in the ORDER BY clause.

     

  • Re: Dynamic Data Entity Framework: Initial Sort Order

    01-19-2009, 6:07 AM
    • Star
      12,288 point Star
    • sjnaughton
    • Member since 04-29-2008, 1:11 PM
    • Newton-le-Willows, Merseyside, UK
    • Posts 2,562
    • TrustedFriends-MVPs

    This could just be a bug an undocumeted feature Big Smile I'll have a look with Northwind and get back

    Steve Big Smile

    Always seeking an elegant solution.
    [Oh! If olny I colud tpye!]
    c# Bits blog
    Oh, and don't forget to mark as answer any posts that help you Big Smile
  • Re: Dynamic Data Entity Framework: Initial Sort Order

    01-19-2009, 6:23 AM
    • Star
      12,288 point Star
    • sjnaughton
    • Member since 04-29-2008, 1:11 PM
    • Newton-le-Willows, Merseyside, UK
    • Posts 2,562
    • TrustedFriends-MVPs

    I don't belive it has anything to do with initial sort, what it does is set the sort on any dropdown list that refer to this column so if you have an other table that you could filter by this on the you DisplayColumn would affect this not the table. hope that makes sense.

    Steve Big Smile

    Always seeking an elegant solution.
    [Oh! If olny I colud tpye!]
    c# Bits blog
    Oh, and don't forget to mark as answer any posts that help you Big Smile
  • Re: Dynamic Data Entity Framework: Initial Sort Order

    01-19-2009, 6:32 AM
    • Star
      12,288 point Star
    • sjnaughton
    • Member since 04-29-2008, 1:11 PM
    • Newton-le-Willows, Merseyside, UK
    • Posts 2,562
    • TrustedFriends-MVPs

    I would create a custom page and add the initial sort to the GridView using the: 

    GridView1.Sort("ColumnName, ColumnName", SortDirection.Descending);
    That should do it.
    Steve Big Smile

    Always seeking an elegant solution.
    [Oh! If olny I colud tpye!]
    c# Bits blog
    Oh, and don't forget to mark as answer any posts that help you Big Smile
  • Re: Dynamic Data Entity Framework: Initial Sort Order

    01-19-2009, 3:51 PM
    • Member
      1 point Member
    • BHunsaker
    • Member since 01-19-2009, 1:54 AM
    • Posts 9

    Thanks for the input.  Is the best option to copy the existing ListDetails.aspx page to a new name and then update the routes in global.asax to use the new jpage? 

  • Re: Dynamic Data Entity Framework: Initial Sort Order

    01-19-2009, 4:15 PM
    Answer
    • Star
      12,288 point Star
    • sjnaughton
    • Member since 04-29-2008, 1:11 PM
    • Newton-le-Willows, Merseyside, UK
    • Posts 2,562
    • TrustedFriends-MVPs

    What you do is much easyer than that:

    1. Create a folder in the ~/DynamicData/CustomPages/ folder by that plural name of the table i.e. Order would be Orders and Category would be Categories
    2. Copy the page you want to customise in the and then edit it to you hearts content it will only be called for the one table

    Big Smile hope that makes sense

    Steve Big Smile

    Always seeking an elegant solution.
    [Oh! If olny I colud tpye!]
    c# Bits blog
    Oh, and don't forget to mark as answer any posts that help you Big Smile
  • Re: Dynamic Data Entity Framework: Initial Sort Order

    01-20-2009, 4:10 AM
    • Member
      1 point Member
    • BHunsaker
    • Member since 01-19-2009, 1:54 AM
    • Posts 9

    Under CustomPages I created a sub-directory named the same as my table (named "EventLog"; the plural form did not work).

    I copied the ListDetails.aspx and code file to the new directory.  In the Page_Load event I added the line:

    GridView1.Sort("datLocalTimestamp", SortDirection.Descending);

    This did the trick.

    The other benefit was that this allowed me to disable editing on this table (comment out the "if (_table.IsReadOnly)" statement allowing the contents of the if block to execute).  I could find no way to disable editing via the metadata code file.  Is there any documentation which will help me figure out which technique (routing, custom pages, metadata attributes) to use for various scenarios.  For example, since I can hide columns with the "ScaffoldColumn(false)" attribute, I believe there should be a way to hide the "Edit/Delete/Insert" control column using attributes.  I believe there should be attributes for the table which would configure the initial sort order.

    Thanks for all the help.

     

  • Re: Dynamic Data Entity Framework: Initial Sort Order

    01-20-2009, 4:34 AM
    • Star
      12,288 point Star
    • sjnaughton
    • Member since 04-29-2008, 1:11 PM
    • Newton-le-Willows, Merseyside, UK
    • Posts 2,562
    • TrustedFriends-MVPs

    You could easily implement doing this via meta data your self by adding you own attribute class: 

    [AttributeUsage(AttributeTargets.Class, AllowMultiple = false)]
    public class DisableEditingAttribute : Attribute
    {
        public Boolean Disable { get; set; }
    
        public DisableEditingAttribute() : this(false) { }
    
        public DisableEditingAttribute(Boolean disable)
        {
            Disable = disable;
        }
    }
    and then test for it in the page: 
    // Disable various options if the table is readonly
    var editingDisabled = table.Attributes.OfType().DefaultIfEmpty(new DisableEditingAttribute()).FirstOrDefault();
    if (table.IsReadOnly || editingDisabled.Disable)
    {
        GridView1.Columns[0].Visible = false;
        InsertHyperLink.Visible = false;
    }
    

    This would allow you to specify disable editing in metadata: 

    [MetadataType(typeof(ProductMD))]
    [DisableEditing(true)]
    public partial class Product
    {
    }
     
    Steve Big Smile

    Always seeking an elegant solution.
    [Oh! If olny I colud tpye!]
    c# Bits blog
    Oh, and don't forget to mark as answer any posts that help you Big Smile
Page 1 of 1 (10 items)