locked
How to set column width for particular column in dynamic data model asp.net .? RRS feed

  • Question

  • User-127625424 posted

    I have to increase the column width in the grid to accomodate the larger values.

    somethign like [column(width = '100px')] .

    Please advise what change needs to be done in meatadata page.

    Friday, June 20, 2014 1:06 AM

Answers

  • User-330204900 posted

    No, I create a CSS class like:

    .Narrow50
    {
        width: 50px;
    }

    Then I use code in the field template:

    /// <summary>
    /// Applies the CSS class to the current field template.
    /// </summary>
    /// <param name="control">The control.</param>
    /// <param name="Column">The column.</param>
    public static void ApplyCss(this FieldTemplateUserControl control)
    {
        var cellCss = control.Column.GetAttribute<CellCssAttribute>();
        var htmlParentCell = control.GetContainerControl<HtmlTableCell>();
        var parentCell = control.GetContainerControl<DataControlFieldCell>();
    
        if (cellCss != null)
        {
            if (parentCell != null)
            {
                var cssClass = parentCell.Attributes["class"];
                if (cssClass != null)
                    cssClass += " " + cellCss.Class;
                else
                    parentCell.Attributes.Add("class", cellCss.Class);
            }
            else
            {
                var cssClass = htmlParentCell.Attributes["class"];
                if (cssClass != null)
                    cssClass += " " + cellCss.Class;
                else
                    htmlParentCell.Attributes.Add("class", cellCss.Class);
            }
        }
    }

    to walk the control tree and add the CSSClass just to that column.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, June 23, 2014 7:53 AM
  • User-330204900 posted

    oops

    /// <summary>
    /// Get the Data Control containing the FiledTemplate
    /// usually a DetailsView or FormView
    /// </summary>
    /// <param name="control">
    /// Use the current field template as a starting point
    /// </param>
    /// <returns>
    /// A FilterRepeater the control that 
    /// contains the current control
    /// </returns>
    public static T GetContainerControl<T>(this Control control) where T : Control
    {
    	var parentControl = control.Parent;
    	while (parentControl != null)
    	{
    		var p = parentControl as T;
    		if (p != null)
    			return p;
    
    		parentControl = parentControl.Parent;
    	}
    	return null;
    }

    and

    /// <summary>
    /// Get the attribute of type T or null if not found
    /// </summary>
    /// <typeparam name="T">Attribute type</typeparam>
    /// <param name="table">Column to search for the attribute on.</param>
    /// <returns>Returns the attribute T or null</returns>
    public static T GetAttribute<T>(this MetaColumn column) where T : Attribute
    {
    	return column.Attributes.OfType<T>().FirstOrDefault();
    }

    That should do it.

    I have that manu extension methods now I forget no-one else has them :(

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, June 23, 2014 10:18 AM

All replies

  • Friday, June 20, 2014 2:01 AM
  • User-127625424 posted

    thanks for the reply. but the link is for simple asp.net application. Mine is a Dynamic data model entity framework. 

    Friday, June 20, 2014 2:21 AM
  • User-330204900 posted

    Hi Ram, there is nothing built in, I created my own attribute and then is the Field template I climb the control tree until I find the Html cell then apply the css class.

    Friday, June 20, 2014 3:53 AM
  • User-127625424 posted

    hi steve, thanks for replying. By giving width in css will affect all the tables right? I need to fix different  width for different tables. Is there any way to acheive.? It will be more helpful, if you can post or mail some sample .

    thanks again 

    Monday, June 23, 2014 7:43 AM
  • User-330204900 posted

    No, I create a CSS class like:

    .Narrow50
    {
        width: 50px;
    }

    Then I use code in the field template:

    /// <summary>
    /// Applies the CSS class to the current field template.
    /// </summary>
    /// <param name="control">The control.</param>
    /// <param name="Column">The column.</param>
    public static void ApplyCss(this FieldTemplateUserControl control)
    {
        var cellCss = control.Column.GetAttribute<CellCssAttribute>();
        var htmlParentCell = control.GetContainerControl<HtmlTableCell>();
        var parentCell = control.GetContainerControl<DataControlFieldCell>();
    
        if (cellCss != null)
        {
            if (parentCell != null)
            {
                var cssClass = parentCell.Attributes["class"];
                if (cssClass != null)
                    cssClass += " " + cellCss.Class;
                else
                    parentCell.Attributes.Add("class", cellCss.Class);
            }
            else
            {
                var cssClass = htmlParentCell.Attributes["class"];
                if (cssClass != null)
                    cssClass += " " + cellCss.Class;
                else
                    htmlParentCell.Attributes.Add("class", cellCss.Class);
            }
        }
    }

    to walk the control tree and add the CSSClass just to that column.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, June 23, 2014 7:53 AM
  • User-127625424 posted

    hi steve, thanks for the demo code.

    Is GetAttributes , GetContainerControl are inbuilt methods or custom methods written  .?

    Monday, June 23, 2014 9:03 AM
  • User-330204900 posted

    oops

    /// <summary>
    /// Get the Data Control containing the FiledTemplate
    /// usually a DetailsView or FormView
    /// </summary>
    /// <param name="control">
    /// Use the current field template as a starting point
    /// </param>
    /// <returns>
    /// A FilterRepeater the control that 
    /// contains the current control
    /// </returns>
    public static T GetContainerControl<T>(this Control control) where T : Control
    {
    	var parentControl = control.Parent;
    	while (parentControl != null)
    	{
    		var p = parentControl as T;
    		if (p != null)
    			return p;
    
    		parentControl = parentControl.Parent;
    	}
    	return null;
    }

    and

    /// <summary>
    /// Get the attribute of type T or null if not found
    /// </summary>
    /// <typeparam name="T">Attribute type</typeparam>
    /// <param name="table">Column to search for the attribute on.</param>
    /// <returns>Returns the attribute T or null</returns>
    public static T GetAttribute<T>(this MetaColumn column) where T : Attribute
    {
    	return column.Attributes.OfType<T>().FirstOrDefault();
    }

    That should do it.

    I have that manu extension methods now I forget no-one else has them :(

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, June 23, 2014 10:18 AM
  • User-127625424 posted

    Thanks a lot Steve :)

    Tuesday, June 24, 2014 1:38 AM
  • User-330204900 posted

    Sorry one thing I did miss out :( was thactual attribute

    [AttributeUsage(AttributeTargets.Property)]
    public class CellCssAttribute : Attribute
    {
        public String Class { get; private set; }
    
        public CellCssAttribute() 
        { 
        }
    
        public CellCssAttribute(String css)
        {
            Class = css;
        }
    }

    Friday, June 27, 2014 5:02 AM