Format column value on a strongly typed DataSet RRS feed

  • Question




    I have a strongly typed DataSet and I want to format the value of all string columns before assigning the value to the column in the datarow. The idea is to trim the incoming value, remove unwanted characters and truncate the string to the maximum length of the column if necessary.

    My idea was writing a method "string FormatString(string value, DataColumn column)" and add it to the setter of the column. That method takes care of trimming, replacing, truncating, etc.

    For a table called MyTable with a column called Owner1FirstNameColumn the code would look like this:



    Code Snippet


    public string Owner1FirstName






    return ((string)(this[this.MyTable.Owner1FirstNameColumn]));


    catch (global::System.InvalidCastException e)


    throw new global::System.Data.StrongTypingException("The value for column \'Owner1FirstName\' in table \'\' is DBNull.", e);





    this[this.MyTable.Owner1FirstNameColumn] = this.FormatStringColumn(value, this.MyTable.Owner1FirstNameColumn);





    That solution works fine, but the problem is that the setter is auto generated. If I change the schema, the code is regenerated and my changes are gone.


    Is there a better aproach for this?

    As a side note, in a future release it would be great to make the autogenerated properties virtual. I could solve this problem by inheriting from the strongly typed dataset and overriding the properties I want to format.



    Wednesday, April 9, 2008 10:00 PM

All replies

  • As you've observed, you can't modify the properties of the auto-generated class, because you don't own them, the DataSet generator does.


    What you can do, though, is handle the DataTable's RowChanging event, and put your formatting logic there.

    Wednesday, April 23, 2008 9:24 PM
  • Hi,

       I wanted to format values of type "double" in typed dataset. I added code to change the format in RowChanged event . But i get this error "Cannot evaluate expression because the current thread is in a stack overflow state"  while loading the data into dataset.

    What does this error mean? Is this related to typed dataset?




    Wednesday, February 16, 2011 9:59 AM
  • A better place to handle this would be in the Binding's Format/Parse event handlers.
    ~~Bonnie Berent [C# MVP]
    Sunday, February 20, 2011 1:32 AM