Data Annotation for column width

橙三吉。 提交于 2021-01-26 22:49:21

问题


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();

回答1:


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().




回答2:


If you look on this page https://documentation.devexpress.com/#WindowsForms/CustomDocument114039

In the section "Validation Attributes", there is a StringLength attribute, where you can specify the min and max number of characters of the data, e.g.

[StringLength(20, MinimumLength = 3)]


来源:https://stackoverflow.com/questions/31983281/data-annotation-for-column-width

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!