views:

640

answers:

1

Hi,

I have a class called SpecialGridView that inherits from GridView.

For other hand I have report pages that are using this SpecialGridView to show data.

The property autogeneratedcolumns was set to true, and I would like to keep this option.

To put the data format, I overrided the "CreateAutoGeneratedColumn" to parse if the data is "Decimal" stablish the format for this type of data. But I'm getting an exception "NotSupportedException"

Any idea about how to solve it?
Thanks in advance.

Here the code that I wrote:

 protected override AutoGeneratedField CreateAutoGeneratedColumn(AutoGeneratedFieldProperties fieldProperties)
 {
     AutoGeneratedField field = new AutoGeneratedField(fieldProperties.DataField);
     field.HtmlEncode = false;
     string name = fieldProperties.Name;
     ((IStateManager)field).TrackViewState();
     field.HeaderText = name;
     field.SortExpression = name;
     field.ReadOnly = fieldProperties.IsReadOnly;
     field.DataType = fieldProperties.Type;

     if (field.DataType == typeof(Decimal))
     {
        field.DataFormatString= "{0:0.00}";
     }
     return field;
 }

Greetings.
Josema

A: 

This is a solution (with reflection):

  protected override AutoGeneratedField CreateAutoGeneratedColumn(AutoGeneratedFieldProperties fieldProperties)
     {
    AutoGeneratedField field = new AutoGeneratedField(fieldProperties.DataField);
                StateBag sb = (StateBag)field.GetType().InvokeMember("ViewState",BindingFlags.GetProperty|BindingFlags.NonPublic|BindingFlags.Instance,null,field, new object[] {});
                field.HtmlEncode = false;
                string name = fieldProperties.Name;
                ((IStateManager)field).TrackViewState();
                field.SortExpression = name;
                field.ReadOnly = fieldProperties.IsReadOnly;
                field.DataType = fieldProperties.Type;
                if (field.DataType == typeof(Decimal))
                {
                   sb["DataFormatString"]= "{0:c}";
                }
                if (field.DataType == typeof(DateTime))
                {
                    sb["DataFormatString"] = "{0:d}";
                }
                return field;
    }
Josema