views:

494

answers:

2

I'm finally digging into Dynamic Data (long overdue). I notice that when I'm viewing the web site, on the main table lists, the primary key field is not displayed.

For example, I have a table that holds Status Codes, with two fields.

StatusCode int identity Primary Key
StatusCodeDescription varchar(25)

On my web site's StatusCodes/List.aspx page, only the StatusCodeDescription shows.

I realize that I can't edit the primary key, but I would like to show it. Is this possible with Dynamic Data?

I can think of some workarounds myself, and in all honesty, I could dig into the documentation further to find it myself, but I'm hoping that someone with more experience knows the answer and can save me some time looking. A link to the appropriate documentation would even be good.

A: 

I found one possible answer myself. I'll post this in case someone else comes looking for the answer in the future. Note that this will only work if the tables all have a one column primary key.

I'll also keep this question out there in case someone else has a better, simpler answer that will work with tables that have multiple primary keys. I'd rather select someone else's answer than my own.

In the List.aspx, I added the following to GridView1.

<asp:TemplateField>
   <HeaderTemplate>
      <%# table.PrimaryKeyColumns[0].Name %>
   </HeaderTemplate>
   <ItemTemplate>
      <%# DataBinder.Eval(Container.DataItem, table.PrimaryKeyColumns[0].Name) %>
   </ItemTemplate>
</asp:TemplateField>
David Stratton
One primary key? Isn't that the definition of a primary key?
Joe Philllips
Sorry - edited this to read "one column primary key" instead of "one primary key" since a primary key can consist of more than one column, and that's what I meant. Thanks for asking, though!
David Stratton
+3  A: 

After spending most of the day researching, I have another answer that is probably more "correct". From what I can see, this is on a per table basis (meaning you'd have to do this once per table)

Using the ScaffoldColumn attribute:

I extended the class that is automatically generated for the table (StatusCodes) using a partial class as follows:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.DynamicData;
using System.ComponentModel.DataAnnotations;

namespace ScholarshipAdmin
{
    [MetadataType(typeof(StatusCodesMetaData))]
    public partial class StatusCodes 
    {

    }

    public class StatusCodesMetaData
    {
     [ScaffoldColumn(true)] 
     public object StatusCode;
    }
}
David Stratton
Scott Kramer
No, I don't use it a lot (yet). When I posted this question I was working on my first project using Dynamic Data.However, I do plan on using it more, where it's appropriate. Mostly for administrative sites, or quick and dirty CRUD applications. Even in my current project, most of the work was done using more standard Asp.Net forms. Only about a tenth of the work being done on the site will be done via the Dynamic Data site. That's probably why appreciated getting it cranked out quickly.
David Stratton
@Scott Kramer - Update: I am now using Dynamic Data on most projects, for at least part of the work. NOw that I'm figuring it out, it's a lot more friendly, and easier to customize.
David Stratton