DataFormatString a CSLA Object

DataFormatString a CSLA Object

Old forum URL: forums.lhotka.net/forums/t/3012.aspx


ksirmons posted on Friday, June 08, 2007

Howdy,

I have a case where I have created a UserInfo CSLA Class that is used for displaying a person’s name.  I have overridden the ToString method to look at a displaystyle enum in the class and to return a string of the users name formatted according to the displaystyle (First Last or Last, First or FiLast etc...)

I have another ToString that takes a displaystyle enum and returns the person’s name in that style.

 

I want to give this ability to the UI.  When using a grid for example, I can select the UserName as a column.  It displays the name using the ToString method and this works well.  It displays whatever the default style is. 

How can I then use the DataFormatString to modify this at the UI layer?

What do I need to add to my CSLA class so it can understand what is typed into the DataFormatString of a <ASP:BoundField>?

 

Aslo, Can you use the DataFormatString against a SmartDate?  I have a smart date object that I want to display as well and it looks like it ignores the formatstring.  If I change the property to give out a date instead of a smartdate, the formatstring works correctly.

Thank you,

Keith

 

 

 

ksirmons replied on Friday, June 08, 2007

It looks like I was able to answer my first question.

I made my class implement IFormattable which created a new ToString method:

Public Overloads Function ToString(ByVal format As String, ByVal formatProvider As System.IFormatProvider) As String Implements System.IFormattable.ToString

I then look at the string values of the format var and send out the person's name based on the format string that I made up.

 FiLi = First Initial Last Initial (JC)

FiLn = First Initial Last Name (JCash)

FnLn = Johnny Cash

LnFn = Cash, Johnny

 

Still pondering if this will work for a SmartDate.

Thank you,

Keith

Brian Criswell replied on Friday, June 08, 2007

You can add the IFormattable interface to SmartDate yourself.  If the SmartDate is empty, return an empty string, otherwise return _date.ToString(format).

Rocky, this may be a sensible addition to SmartDate.

RockfordLhotka replied on Saturday, June 09, 2007

Added to the wish list.

ksirmons replied on Monday, June 11, 2007

Brian,

Where would I add this functionality?  Into the CSLA framework code and compile? I think that would be bad since I would not want to re-modify every framework update to fix my code.

Should I create a custom date object that inherits from SmartDate to add this functionality into?

Thank you,

Keith

Brian Criswell replied on Monday, June 11, 2007

Well, I personally modify a few paerts of the framework directly.  I store my copy of CSLA in Subversion.  When Rocky releases a new version, I unzip it into my repository.  This causes my customisations to be overwritten, so I diff all of the changed files and inlude my modifications back in.  Finally, I add all of the new files to Subversion and commit.  Total time from start to finish : 15-30 minutes.

In this case, I would suggest adding IFormattable directly to SmartDate and implement it using the algorithm suggested above.

Copyright (c) Marimer LLC