views:

618

answers:

1

I have a number of tables with lots of columns that I'm using Dynamic Data with. I only want to show a small subset of the columns.

Currently when Dynamic Data scaffolds a table it creates all the columns unless I set [ScaffoldColumn(false)] to false. This works but as I only want to display a small subset of the columns it would be easier to turn them on rather than off.

Is there a way to change the default behaviour so a column is not scaffolded unless I tell it to?

+3  A: 

I don't know of a way to do this out of the box. However, you can create your own custom attribute to do this.

Here's some samples on how to create a custom attribute to control how DD works:

http://csharpbits.notaclue.net/2009/04/hiding-foreign-key-column-globally-in.html - Sample of creating an attribute to hide the foreign key column for all tables.

http://csharpbits.notaclue.net/2008/10/dynamic-data-hiding-columns-in-selected.html - Sample of another attribute to hide a column but applied at the page level.

I see the first one where it's defined at the table level perhaps your best bet. You could create your own attribute say HideFieldsByDefault and set it to true.

You would then add your fields that you want to be shown with ScaffoldColumn to true and in the code for the IAutoFieldGenerator's GenerateFields method you would then look for your custom attribute and code accordingly and if your attribute wasn't their then you would just use the default DD way.

You might also be able to do this with your own metadata provider:

http://mattberseth.com/blog/2008/08/dynamic%5Fdata%5Fand%5Fcustom%5Fmetada.html

On this one, I'm not sure exactly how you could do it with a custom provider but perhaps a little research will reveal it as a good option.

klabranche
Thanks for your answer. I'll give this a go if no one comes back with an out of the box way to do it.
Richard
I'm pretty sure this would work, but this was a bit complicated for me. So for this and a few other reasons I've decided to use .NET MVC instead. It's just a better match for the project I'm working on.
Richard