Data annotations

You can custome Grid and columns setting, using data annotations. In other words you can mark properties of you model class as grid columns, specify column options and call "AutoGenerateColumns" method. Grid.Mvc automatically create columns as you describe in your annotations.

There are some attributes for this:
  1. GridTableAttribute - applies for model class and specify options for Grid (paging options...);
  2. GridColumnAttribute - applies for model public property and tell that this property is grid column with set of properties;
  3. GridHiddenColumn - applies for model public property and tell that this property is grid hidden column;
  4. NotMappedColumnAttribute - applies for model public property and tell that this property is NOT a column. If property has this attribute - Grid.Mvc will not Add this column to column collection;

For example model class with data annotations:

[GridTable(PagingEnabled = true, PageSize = 20)]
public class Foo
{
    [GridColumn(Title = "Name title", SortEnabled = true, FilterEnabled = true)]
    public string Name { get; set; }

    [GridColumn(Title = "Active Foo?")]
    public bool Enabled { get; set; }

    [GridColumn(Title = "Date", Format = "{0:dd/MM/yyyy}")]
    public DateTime FooDate { get; set; }

    [NotMappedColumn]
    public byte[] Data { get; set; }
}

This class describe that grid table must contains 3 columns (Name, Enabled and FooDate) with custom options.

Than you can render this table in the View:

@Html.Grid(Model).AutoGenerateColumns()

Grid.Mvc generate columns based on your data annotations when AutoGenerateColumns method is invoked. It means that you can add custom columns after or before this method, for example:

@Html.Grid(Model).AutoGenerateColumns().Columns(columns=>columns.Add(foo=>foo.Child.Price))

Also you can overwrite grid options using WithPaging method.

Last edited Oct 31, 2012 at 2:09 PM by Bukharin, version 2