开发者

Displaying two fields in lookup

Introduction first, question at the end. Please read carefully!


I have a master-detail relation between two tables:

CREATE TABLE [dbo].[LookupAttributes] (
    [Id] int IDENTITY (1, 1) NOT NULL, 
    [Name] nvarchar (255) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL) ;
ALTER TABLE [dbo].[LookupAttributes] ADD CONSTRAINT [PK_LookupAttributes] PRIMARY KEY ([Identity]) ; 

CREATE TABLE [dbo].[Lookup] (
    [Id] int IDENTITY (1, 1) NOT NULL, 
    [LookupAttributesLink] int NOT NULL, 
    [Code] nvarchar (20) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL, 
    [Value] nvarchar (80) COLLATE SQL_Latin1_General_CP1_CI_AS NULL) ;
ALTER TABLE [dbo].[Lookup] ADD CONSTRAINT [IX_Lookup] UNIQUE ([LookupAttributenLink], [Code]) ; 

(There are more fields and indices in both tables but these are the ones that matter...)

The project I'm working on is meant to maintain data in 50+ tables, and every week this data is exported to XML to be used by some deskt开发者_StackOverflow中文版op application as source data. While I wanted to make this a pretty-looking application, it just needed to be done fast, thus I use a Dynamic Data Site so the data can be maintained. It works just fine, except for this table...

As it turns out, there are 600 different lookup records that share the same code, but different attributes. The DDS displays attribute and code correctly in the list of lookup records so there never was any confusion about which lookup record someone was editing. And this has been in use for over 2 years now.


Now the problem: A new table "Lookup-Override" has been added which links to the [Id] field of the Lookup table. Each record in this new table thus displays the [Code] field, but since [Code] isn't unique, it's unclear which Override record belongs to which Lookup record.

To solve this, I need to display more information from the Lookup record. Since the only unique set of fields is the attribute plus code, I need to display both. But displaying [LookupAttributesLink]+[Code] isn't an option either, since [LookupAttributesLink] is just a number.

I need the DDS to display [Attributes].[LookupAttributesLink]+[Lookup].[Code] in a single column. Question is: how?

I've considered adding a calculated field to the Lookup table, but I cannot get the attribute name that way.

I could create a special page to maintain this table but I don't like that solution either, since it "breaks" the DDS principle in my opinion. I'm trying to avoid such pages.

So, any other possibilities to get the site display both attribute name and lookup code in the override table?


The most interesting solution would be by using a calculated field which could retrieve the attribute name. How to do that?


Solved it myself! See answer below, which works just fine.


Found it! I had to do a few things:

CREATE FUNCTION LookupName (
  @Attr int,
  @Code nvarchar(255)
) RETURNS nvarchar(1000)
AS
BEGIN
 DECLARE @Name nvarchar(1000)
 SELECT @Name = Name
 FROM [dbo].[LookupAttributes]
 WHERE [Id]=@Attr;
 RETURN @Name + '/' + @Code;
END
GO
ALTER TABLE [dbo].[lookup] ADD [Name] AS [dbo].[LookupName]([LookupAttributesLink], [Code])
GO

This will add an additional calculated field to the table which uses a function to calculate the proper name. I then had to add some metadata for the lookup table:

[MetadataType(typeof(LookupMetadata))]
public partial class Lookup { }
[DisplayColumn("Name", "Name")]
[DisplayName("Lookup")]
public class LookupMetadata
{
    [ScaffoldColumn(false)]
    public int Id;
    [ScaffoldColumn(false)]
    public object Name;
}

This will hide the Name column from the Lookup table itself, but it makes it visible for the Override table. (And it will be used to display the proper value.
Done this, solved the problem! Quite easy, actually. :-)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜