5

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.

David
  • 72,686
  • 18
  • 132
  • 173

3 Answers3

6

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
  • 72,686
  • 18
  • 132
  • 173
  • do you use dynamic data alot? I found it maddening!!! This was the first thing I needed to do! ( show the pri key ) people thought I was crazy when they asked for it & I could not display it – Scott Kramer Oct 10 '09 at 04:39
  • 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 Oct 10 '09 at 05:09
  • @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 Jul 28 '10 at 20:01
  • This solution also works for columns of types that aren't shown by default, such as Guid (uniqueidentifier in SQL Server). – Patrick Szalapski May 16 '11 at 19:58
0

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
  • 72,686
  • 18
  • 132
  • 173
  • One primary key? Isn't that the definition of a primary key? – Joe Phillips Sep 29 '09 at 13:47
  • 1
    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 Sep 29 '09 at 13:50
0

Just in case anyone else ends up here in this 10 year old thread like me, I am using Linq to SQL with DyanamicData and I was able to solve this by changing "Auto Generated Value" to False in the O/R Designer properties for that column

One potential pitfall that I have seen so far is that when trying to insert a row, the user is still prompted for that field

DreadedEntity
  • 133
  • 1
  • 7