Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Data Annotation for column width

I'm binding a collection of objects to a DevExpress GridControl, and using 15.1 Data Annotations to customise the look. However I'm struggling to find anything about setting column size of a property. Is this possible through annotations?

Class with annotations:

public class DataFeedback
{
    [Display(Name = "Row Num", Order = 0)]
    public int RowNum { get; set; }
    [Display(Name = "Description", Order = 1)]
    public string Desc { get; set; }

    public DataFeedback(int rowNum, string desc)
    {
        RowNum = rowId;
        Desc = desc;
    }
}

Simple Binding

var feedbackList = new List<DataFeedback>()
feedbackList.Add(new DataFeedback(1, "test"))
gridControl1.DataSource = feedbackList;

// only layout I've found so far
gridView1.BestFitColumns();
like image 928
wonea Avatar asked Aug 13 '15 08:08

wonea


1 Answers

Out of the box there are no data annotation attributes that can be used to specify the column size of the UI grid. The StringLength attribute (and others) are used to specify the column size in the database and the size of the data for data validation, but that's as far as they'll go.

I'm not familiar with the DevExpress controls, but if it provides a hook in to the automatic column generation process you can do something similar to what I did for the Telerik grid (http://geekswithblogs.net/sdorman/archive/2015/11/05/kendo-grid-mvc-wrapper-automatic-column-configuration.aspx).

In that solution, I created a custom data annotations attribute (similar to this):

public class GridColumnAttribute : Attribute, IMetadataAware
{
    public const string Key = "GridColumnMetadata";

    public string Width { get; set; }

    public void OnMetadataCreated(ModelMetadata metadata)
    {
        metadata.AdditionalValues[GridColumnAttribute.Key] = this;
    }
}

Then, you decorate your view model:

public class DataFeedback
{
    [Display(Name = "Row Num", Order = 0)]
    [GridColumn(Width = "100px")]
    public int RowNum { get; set; }

    [Display(Name = "Description", Order = 1)]
    [GridColumn(Width = "300px")]
    public string Desc { get; set; }   
 }

Finally, in the code that gets called from your column generation hook, you would do something similar to this:

public static void ConfigureColumn<T>(GridColumnBase<T> column) where T : class
{
   CachedDataAnnotationsModelMetadata metadata = ((dynamic)column).Metadata;
   object attributeValue = null;
   if (metadata.AdditionalValues.TryGetValue(GridColumnAttribute.Key, out attributeValue))
   {
      var attribute = (GridColumnAttribute)attributeValue;
      column.Width = attribute.Width;
   }
}

It looks like you might be able to do this by using the supported fluent API and the With<T> extension method and/or possibly hooking in to the RowCellStyle event. (https://documentation.devexpress.com/#WindowsForms/CustomDocument18017)

If you can't hook in to the column generation process, you may be able to do the same thing but using your own extension method that gets called after the grid is bound, much like you're doing with calling BestFitColumns().

like image 59
Scott Dorman Avatar answered Sep 30 '22 14:09

Scott Dorman