views:

56

answers:

2

I'm a novice programmer and I've been doing it more as a hobby then a career. As such I'm putting together an application for generating invoices for company I work for. I have the invoicing stuff working which takes in data feeds from our suppliers and matches up customers, products and rates etc. and produces the invoices. I'm now trying to pretty it up and allow for the customer and rate details to be edited. Part of that means I've got a number of tables that hold lookup values, e.g. VAT rate, mark up value, custom rates.

I'm writing the application in WPF, C#, .Net 4. I'm also trying to keep to the MVVM pattern for separating the layers.

I've created a generic VeiwModel called EditLookupTableViewModel which handles talking to the data layer and all the rest. My problem is with the View. I would like to create a single view that can handle all the data types that I allow on the above class. The bit I'm stuck on is this.

I would like to present the data as a list of the current values in the database table and allow people to edit those values, or add new values. The various data types are all based on tables in an SQL database being access via Entity Framework 4. The tables do not all have the same number or type of columns.

If I present the data as a list using a DataGrid control, how do I change the column types based on the data type being displayed?

I've been searching on various sites but have not found anything that fits.

I thought I'd be able to user a DataTemplate and then use a DataTemplateSelector but I don't see any way of creating a DataTemplate for a DataGrid.

I then thought I'd use a ContentPresenter but I can't see how you'd select the template.

Ideally I would like to use some kind og TemplateSelector as I could then control the template from code as several tables have the same structure and so it would be great to reuse the template for those tables that are the same.

Any help would be much appreciated as I'm sure there must be a more elegant solution than creating multiple views.

Thanks

Simon

A: 

I would look into WCF RIA though it would require some changes to how you interact with the data. Basically, you have an Entity Model that handles the persistent state you need for CRUD operations, and a pretty silverlight datagrid where you can edit a cell at a time. There are samples on the Net, and I can find one if you need that take less than 30 minutes to get a sample up and running.

websch01ar
Thanks for the quick response. That may well be a solution but I don't want to get into using WCF at this point. Also I don't want to use Silverlight. The platform is WPF and it will be staying that way for reasons outside of the scope of this post.What I would like is for the DataGrid to change the number and type of it's columns based on the type of object I pass to it but I don't want to leave it to auto generate those columns as I don't necessarily want to display all columns all the time.
SBuckner
A: 

This answer might help you. It enables adding and removal of columns sort of MVVM style.

Note that answer binds to a collection of DataGridColumns.I have modified it to bind to my ColumnViewModel.This way I feel my solution is more MVVM as it does not require using a control namepsace in my ViewModel code.

You can do similar based on that answer, and possibly create different sub-classes of your ColumnViewModel to bind to and use GetType within the extension to support your needs noted in the question.

Martino