Imagine I have a Flyer table with 2 colunms in my SQl Server:
FlyerID (int)(PK)
FlyerDate (smalldatetime)
FlyerID is a foreign key on other tables, for example Store table. Store has 3 columns:
StoreID (int)(PK)
Name (nvarchar)
FlyerID (int)(FK) relationship with Flyer table
Now, at the DynamicData site, we will have the Stores page and the Flyers
page. I want to display FlyerDate using my custom format. The format is
MMM-dd-yyyy, for example.
At the Flyers page, the way I implemented following the tutorial video at:
http://www.asp.net/learn/3.5-SP1/video-291.aspx works perfectly, displaying my custom format for the FlyerDate column.
At the Stores page however, the Flyer column values (which are hyperlinks) don't show the date in my custom format. Also, in the Filter (dropdown list) doesn't show custom format.
You are better overriding the ToString method like this:
[MetadataType(typeof(FlyerMetadata))]
public partial class Flyer
{
public override string ToString()
{
return this.CreatedDate = DateTime.Now.ToString("MMM-dd-yyyy");
}
}
if you want a field in the DB set to the dateTime it was created then you could do this by overriding the table Insert method (I am assuming you are using Linq to SQL and not Entity Framework) like this:
public partial class NorthwindDataContext
{
partial void InsertCustomer(Customer instance)
{
instance.CreationDate = DateTime.Now;
this.ExecuteDynamicInsert(instance);
}
}
And then you could just display the CreationDate filed in you DDL the other advantage is that custom fields will not sort on because the sort is done in the DB so if you have a DB field like above then you can sort on it and get the effect you want.
Dynamic Data
See my blog C# Bits | Twitter @sjnaughton Always seeking an elegant solution.
I didn't mean I fixed my problem. I meant I fixed my post because you were not understading my problem. Could you please read again my post from the beggining or do you want me to create a new topic?
Hi Filhodapuc, if you override the ToString method of your Flyer class you shouls get the dropdown list containing the Data formattes as you required the you could add the DisplayColumn Attribute like below:
[DisplayColumn("DisplayDate", "FlyerDate", true)]
[MetadataType(typeof(FlyerMetadata))]
public partial class Flyer
{
public override string ToString()
{
return FlyerDate.ToString("MMM-dd-yyyy");
}
public class FlyerMetadata
{
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:MMM-dd-yyyy}")]
public DateTime FlyerDate { get; set; }
}
}
I know this is the same as I have shown before I have just tested it with Northwind DB and it works fine see:
Hope this helps [:D]
Dynamic DataOverride ToString
See my blog C# Bits | Twitter @sjnaughton Always seeking an elegant solution.
Thank you! This is beautiful and yet so simply. I didn't see clear before cause you used the this.CreateDate in it, but i get it now. Thank you!
sjnaughton
Hi Filhodapuc, if you override the ToString method of your Flyer class you shouls get the dropdown list containing the Data formattes as you required the you could add the DisplayColumn Attribute like below:
filhodapuc
Member
9 Points
30 Posts
DisplayFormatAttribute doesn't work when this is a ForeignKey DropDownList / Hyperlink
Jan 07, 2010 08:50 PM|LINK
What i want to do:
Imagine I have a Flyer table with 2 colunms in my SQl Server:
FlyerID is a foreign key on other tables, for example Store table. Store has 3 columns:
Now, at the DynamicData site, we will have the Stores page and the Flyers page. I want to display FlyerDate using my custom format. The format is MMM-dd-yyyy, for example.
At the Flyers page, the way I implemented following the tutorial video at: http://www.asp.net/learn/3.5-SP1/video-291.aspx works perfectly, displaying my custom format for the FlyerDate column.
At the Stores page however, the Flyer column values (which are hyperlinks) don't show the date in my custom format. Also, in the Filter (dropdown list) doesn't show custom format.
Suggested Failed Solution:
At: http://ericphan.info/development/asp-net-dynamic-data-display-custom-text-in-a-foreign-key-dropdownlist-combobox. Giving me error "The display column 'DisplayDate' specified for the table does not exist."
I'm using .Net Framework 4.0 BETA. I'm using Entity Framework.
Code:
[DisplayColumn("DisplayDate", "FlyerDate", true)]
[MetadataType(typeof(FlyerMetadata))]
public partial class Flyer
{
[ScaffoldColumn(true)]
public string DisplayDate
{
get { return this.FlyerDate.ToString("MMM-dd-yyyy"); }
}
}
public class FlyerMetadata
{
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:MMM-dd-yyyy}")]
public DateTime FlyerDate { get; set; }
}
What should I do? Please help! Thank you!
DisplayFormatAttribute ASP .NET Dynamic Data .NET Framework 4.0
sjnaughton
All-Star
27387 Points
5483 Posts
MVP
Re: DisplayFormatAttribute doesn't work when this is a ForeignKey DropDownList / Hyperlink
Jan 08, 2010 07:22 AM|LINK
You are better overriding the ToString method like this:
[MetadataType(typeof(FlyerMetadata))] public partial class Flyer { public override string ToString() { return this.CreatedDate = DateTime.Now.ToString("MMM-dd-yyyy"); } }if you want a field in the DB set to the dateTime it was created then you could do this by overriding the table Insert method (I am assuming you are using Linq to SQL and not Entity Framework) like this:
public partial class NorthwindDataContext { partial void InsertCustomer(Customer instance) { instance.CreationDate = DateTime.Now; this.ExecuteDynamicInsert(instance); } }And then you could just display the CreationDate filed in you DDL the other advantage is that custom fields will not sort on because the sort is done in the DB so if you have a DB field like above then you can sort on it and get the effect you want.
Dynamic Data
Always seeking an elegant solution.
filhodapuc
Member
9 Points
30 Posts
Re: DisplayFormatAttribute doesn't work when this is a ForeignKey DropDownList / Hyperlink
Jan 08, 2010 04:48 PM|LINK
Please read again my post. I'm sorry for my poor explanation before, it was bad. But did you understand now?
sjnaughton
All-Star
27387 Points
5483 Posts
MVP
Re: DisplayFormatAttribute doesn't work when this is a ForeignKey DropDownList / Hyperlink
Jan 08, 2010 05:29 PM|LINK
OK so your Metadata could look somtning like:
[DisplayColumn("DisplayDate", "FlyerDate", true)] [MetadataType(typeof(FlyerMetadata))] public partial class Flyer { public override string ToString() { return this.CreatedDate = FlyerDate .ToString("MMM-dd-yyyy"); } public class FlyerMetadata { [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:MMM-dd-yyyy}")] public DateTime FlyerDate { get; set; } } }I'll give this a test when I get back in later, but I think it is correct [:D]
Dynamic Data ToString Override Metadata classes
Always seeking an elegant solution.
filhodapuc
Member
9 Points
30 Posts
Re: DisplayFormatAttribute doesn't work when this is a ForeignKey DropDownList / Hyperlink
Jan 08, 2010 06:11 PM|LINK
Please read again my post. I fixed.
sjnaughton
All-Star
27387 Points
5483 Posts
MVP
Re: DisplayFormatAttribute doesn't work when this is a ForeignKey DropDownList / Hyperlink
Jan 08, 2010 08:36 PM|LINK
No problem glad you fixed it [:)]
Dynamic Data
Always seeking an elegant solution.
filhodapuc
Member
9 Points
30 Posts
Re: DisplayFormatAttribute doesn't work when this is a ForeignKey DropDownList / Hyperlink
Jan 08, 2010 10:38 PM|LINK
Sorry Steve,
I didn't mean I fixed my problem. I meant I fixed my post because you were not understading my problem. Could you please read again my post from the beggining or do you want me to create a new topic?
sjnaughton
All-Star
27387 Points
5483 Posts
MVP
Re: DisplayFormatAttribute doesn't work when this is a ForeignKey DropDownList / Hyperlink
Jan 08, 2010 11:05 PM|LINK
I'll read through it again and try and get it this time [:D]
Dynamic Data
Always seeking an elegant solution.
sjnaughton
All-Star
27387 Points
5483 Posts
MVP
Re: DisplayFormatAttribute doesn't work when this is a ForeignKey DropDownList / Hyperlink
Jan 16, 2010 01:25 PM|LINK
Hi Filhodapuc, if you override the ToString method of your Flyer class you shouls get the dropdown list containing the Data formattes as you required the you could add the DisplayColumn Attribute like below:
[DisplayColumn("DisplayDate", "FlyerDate", true)] [MetadataType(typeof(FlyerMetadata))] public partial class Flyer { public override string ToString() { return FlyerDate.ToString("MMM-dd-yyyy"); } public class FlyerMetadata { [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:MMM-dd-yyyy}")] public DateTime FlyerDate { get; set; } } }I know this is the same as I have shown before I have just tested it with Northwind DB and it works fine see:
Hope this helps [:D]
Dynamic Data Override ToString
Always seeking an elegant solution.
filhodapuc
Member
9 Points
30 Posts
Re: DisplayFormatAttribute doesn't work when this is a ForeignKey DropDownList / Hyperlink
Jan 16, 2010 02:15 PM|LINK
Thank you! This is beautiful and yet so simply. I didn't see clear before cause you used the this.CreateDate in it, but i get it now. Thank you!