2

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

1 Answer 1

0

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;
    }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.