locked
DynamicField within GridView truncating data RRS feed

  • Question

  • User151714553 posted

    When I use a DynamicField within aGridView the data displayed in the column is truncated. How can I control this behaviour?

    Monday, June 28, 2010 1:06 AM

Answers

  • User-330204900 posted

    Hi Simon, if the field is a long test field you may need to add a UIHint to the override the field template it is using.

    I would put a break point in each of the possible field templates and then debug to see which is being used, or use F12 in IE8 to get the Dev tools and look at the column to see how it is being rendered, as this bould be a css issue (i.e. overflow set to hidden)

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, June 28, 2010 5:18 AM
  • User-330204900 posted

    Hi Yuipcheng, David Fowler gave me the solution, which is to create a class based on the DefaultAutoFieldGenerator and override the CreateField method:

    /// <summary>
    /// Add the option to the Default Auto Field Generator 
    /// to not add the (Style="white-space: nowrap;") to each field.
    /// </summary>
    public class AdvancedAutoFieldGenerator : DefaultAutoFieldGenerator
    {
        private Boolean _noWrap;
    
        /// <summary>
        /// Initializes a new instance of the 
        /// <see cref="AdvancedAutoFieldGenerator"/> class.
        /// </summary>
        /// <param name="table">The table.</param>
        /// <param name="noWrap">if set to <c>true</c> 
        /// the (Style="white-space: nowrap;") is added 
        /// to each field.</param>
        public AdvancedAutoFieldGenerator(MetaTable table, Boolean noWrap)
            : base(table)
        {
            _noWrap = noWrap;
        }
    
        protected override DynamicField CreateField(
            MetaColumn column, 
            ContainerType containerType, 
            DataBoundControlMode mode)
        {
            DynamicField field = base.CreateField(column, containerType, mode);
            field.ItemStyle.Wrap = !_noWrap;
            return field;
        }
    }

    In the CreateFiedl method we call the base and then set the field.ItemStyle.Wrap value to turn off the nowrap feature.

    I decided that I wanted to be able to turn this off or leave it on so I added a parameter to the contstuctor for this.

    We use this on the List page in the Page_Load event (if we do it6 any earlier then the DefaultAutoFieldGenerator will still be applied instead) like this:

    // apply custom auto field generator
    GridView1.ColumnsGenerator = new AdvancedAutoFieldGenerator(table, false);

    setting the noWrap parameter to false will turn off the feature wich adds the style="white-space:nowrap;" to each table cell and row.

    Hope this helps [:)]

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, November 14, 2010 6:27 AM

All replies

  • User-629546587 posted

    hi,

       Please check datasource function you are binding the gridview, whether it is returning full data or it is getting truncated in 

    the function or query itself.

    Monday, June 28, 2010 3:43 AM
  • User1945980297 posted

    There can be multiple reasons for that to be...  Is the max length property set for the text field you are using in the gridview ???

    Monday, June 28, 2010 3:45 AM
  • User151714553 posted

    The data isn't truncated in the source. If I use a BoundField instead of a DynamicField the data isn't truncated.

    Cheers

     

    Monday, June 28, 2010 4:18 AM
  • User151714553 posted

    The DynamicField control doesn't have a max length property. 

    Monday, June 28, 2010 4:20 AM
  • User-330204900 posted

    Hi Simon, if the field is a long test field you may need to add a UIHint to the override the field template it is using.

    I would put a break point in each of the possible field templates and then debug to see which is being used, or use F12 in IE8 to get the Dev tools and look at the column to see how it is being rendered, as this bould be a css issue (i.e. overflow set to hidden)

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, June 28, 2010 5:18 AM
  • User151714553 posted

    Thanks for pointing me in the right direction Steve. The issue was simply due to the MAX_DISPLAYLENGTH_IN_LIST constant in the default Text field template. Exactly where it should be Embarassed

     

    Now all I need is an attribute to replace this constant. Don't suppose you've got one of those up your sleeve Laughing

     

    Thanks, Simon

    Monday, June 28, 2010 7:14 AM
  • User-330204900 posted

    Hi Simon, yes I do [:)]

    [AttributeUsage(AttributeTargets.Property, AllowMultiple = false)]
    public class FieldOptionsAttribute : Attribute
    {
        /// <summary>
        /// Gets or sets the rows.
        /// </summary>
        /// <value>The rows.</value>
        public int Rows { get; set; }
        /// <summary>
        /// Gets or sets the columns.
        /// </summary>
        /// <value>The columns.</value>
        public int Columns { get; set; }
    }
    

    And then change your Test.aspx page to this.

    public partial class TextField : System.Web.DynamicData.FieldTemplateUserControl
    {
        private const int MAX_DISPLAYLENGTH_IN_LIST = 25;
    
        public override string FieldValueString
        {
            get
            {
                string value = base.FieldValueString;
                if (ContainerType == ContainerType.List)
                {
                    var fieldOptions = MetadataAttributes.OfType<FieldOptionsAttribute>().FirstOrDefault();
                    int maxColumns;
                    if (fieldOptions != null && fieldOptions.Columns > 0)
                        maxColumns = fieldOptions.Columns;
                    else
                        maxColumns = MAX_DISPLAYLENGTH_IN_LIST;
    
                    if (value != null && value.Length > maxColumns)
                        value = value.Substring(0, maxColumns - 3) + "...";
                }
                return value;
            }
        }
    
        public override Control DataControl
        {
            get { return Literal1; }
        }
    
    }

    Shouls work, I also use the Row for the MultilineText_Edit field to alter how many rows it has.
     

    Tuesday, June 29, 2010 6:37 AM
  • User151714553 posted

    Thanks Steve. I jumped the gun and built one myself yesterday - my first metadata attribute. Pretty similar to yours except you also have the Row parameter which should be useful. I'll include mine here because I also implemented it in the ForeignKey field template, which might be of interest to someone.

    [AttributeUsage(AttributeTargets.Property, AllowMultiple = false)]
    public class MaxDisplayLengthInListAttribute : Attribute
    {
        public Int16 Length { get; private set; }
    
        public MaxDisplayLengthInListAttribute(Int16 length)
        {
            Length = length;            
        }
    }


    Text.ascx

    public override string FieldValueString
    {
        get
        {
            string value = base.FieldValueString;
            if (ContainerType == ContainerType.List)
            {
                Int16 MaxLength = 25;
                var metadata = MetadataAttributes.OfType<MaxDisplayLengthInListAttribute>().FirstOrDefault();
                if (metadata != null)
                {
                    MaxLength = metadata.Length;
                }
                if (value != null && value.Length > MaxLength)
                {
                    value = value.Substring(0, MaxLength - 3) + "...";
                }
            }
            return value;
        }
    }

    ForeignKey.ascx

    protected string GetDisplayString()
    {
        object value = FieldValue;
    
        if (value == null)
        {
            return FormatFieldValue(ForeignKeyColumn.GetForeignKeyString(Row));
        }
        else
        {
            string stringValue = FormatFieldValue(ForeignKeyColumn.ParentTable.GetDisplayString(value));
            if (ContainerType == ContainerType.List)
            {
                Int16 MaxLength = 20;
                var metadata = MetadataAttributes.OfType<MaxDisplayLengthInListAttribute>().FirstOrDefault();
                if (metadata != null)
                {
                    MaxLength = metadata.Length;
                }
                if (stringValue.Length > MaxLength)
                {
                    stringValue = stringValue.Substring(0, MaxLength - 3) + "...";
                }
            }
            return stringValue;
        }
    }




     


     

     

     

     

     

     

    Tuesday, June 29, 2010 9:07 PM
  • User-330204900 posted

    Excelant job Simon, I'll certanly look at you sample [:)]

    Wednesday, June 30, 2010 5:05 AM
  • User-1672068718 posted

    I commented out the code to truncate the FieldValueString, but the rendered text doesn't wrap since the td tag has style="white-space: nowrap;" 

    There is no code to set the wrap = false anywhere.

    Why is this happening?

    Wednesday, November 10, 2010 7:48 PM
  • User-1672068718 posted

    i found a workaround by adding the following css code to List.aspx, but i rather get rid of the extra style code from the td tag.

    td

    {

      white-space:normal;

    }

    Wednesday, November 10, 2010 7:58 PM
  • User-330204900 posted

    Hi Yuipcheng, it would appear that DD is adding this and I can't find a way around it yet [:(]

    Wednesday, November 10, 2010 9:16 PM
  • User-330204900 posted

    Hi Yuipcheng, David Fowler gave me the solution, which is to create a class based on the DefaultAutoFieldGenerator and override the CreateField method:

    /// <summary>
    /// Add the option to the Default Auto Field Generator 
    /// to not add the (Style="white-space: nowrap;") to each field.
    /// </summary>
    public class AdvancedAutoFieldGenerator : DefaultAutoFieldGenerator
    {
        private Boolean _noWrap;
    
        /// <summary>
        /// Initializes a new instance of the 
        /// <see cref="AdvancedAutoFieldGenerator"/> class.
        /// </summary>
        /// <param name="table">The table.</param>
        /// <param name="noWrap">if set to <c>true</c> 
        /// the (Style="white-space: nowrap;") is added 
        /// to each field.</param>
        public AdvancedAutoFieldGenerator(MetaTable table, Boolean noWrap)
            : base(table)
        {
            _noWrap = noWrap;
        }
    
        protected override DynamicField CreateField(
            MetaColumn column, 
            ContainerType containerType, 
            DataBoundControlMode mode)
        {
            DynamicField field = base.CreateField(column, containerType, mode);
            field.ItemStyle.Wrap = !_noWrap;
            return field;
        }
    }

    In the CreateFiedl method we call the base and then set the field.ItemStyle.Wrap value to turn off the nowrap feature.

    I decided that I wanted to be able to turn this off or leave it on so I added a parameter to the contstuctor for this.

    We use this on the List page in the Page_Load event (if we do it6 any earlier then the DefaultAutoFieldGenerator will still be applied instead) like this:

    // apply custom auto field generator
    GridView1.ColumnsGenerator = new AdvancedAutoFieldGenerator(table, false);

    setting the noWrap parameter to false will turn off the feature wich adds the style="white-space:nowrap;" to each table cell and row.

    Hope this helps [:)]

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, November 14, 2010 6:27 AM
  • User-1672068718 posted

    Steve,

    that works!

    Thank you! 

    yuipcheng

    Thursday, November 18, 2010 8:03 PM