locked
Order columns of MetaTable by "Order" attribute? RRS feed

  • Question

  • User-191850887 posted

    Using Dynamic Data I have been able to create custom attributes, and now I am trying to use the MetaTable in code-behind (c#) to create a checkboxlist. The code below should do the trick, but the problem I am having is that all "foreign key" columns are put at the end. This happens even when I use attributes such as "[Display(Order = 70)]" in the corresponding class. Apparently this attribute only changes the "display" order of the columns, not the order of the columns on a data layer level. How can I order the columns within the MetaTable using the "Display Order" attribute? It seems like a simple task, but I have no idea how to do it. Is there any quick and easy way to achieve this? Or should I be using another approach?

    Thanks in advance for any help!

        protected MetaTable table;
    
        protected void Page_Init(object sender, EventArgs e) {
            table = DynamicDataRouteHandler.GetRequestMetaTable(Context);
            // removed other code
        }
    
            foreach(MetaColumn c in table.Columns){
                if (c.Scaffold)
                {               
                    CheckBoxList1.Items.Add(c.DisplayName);               
                    // removed other code where I use a custom attribute to determine whether each checkbox is "selected" by default or not                         
               i++;
                }     
            } 

     

    Tuesday, March 29, 2011 5:16 AM

Answers

  • User-191850887 posted

    After several hours of trying I finally found something that worked. In case someone else is having the same problem, here is the code that ended up working for me (code in italics is what I added):

     

        protected MetaTable table;
    
        protected void Page_Init(object sender, EventArgs e) {
            table = DynamicDataRouteHandler.GetRequestMetaTable(Context);
            // removed other code
        }
    
            DisplayAttribute defaultdisplay = new DisplayAttribute { Order = 0 };
            var columns = table.Columns.OrderBy(column => column.Attributes.OfType<DisplayAttribute>().DefaultIfEmpty(defaultdisplay).First().GetOrder());
            int i = 0;
    
            foreach(MetaColumn c in columns){
                if (c.Scaffold)
                {               
                    CheckBoxList1.Items.Add(c.DisplayName);               
                    // removed other code where I use a custom attribute to determine whether each checkbox is "selected" by default or not                         
               i++;
                }     
            } 

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, March 30, 2011 3:11 AM