In my last post I was talking about my idea to use the jqGrid to create a grid based on the data model. In this post I want to take the existing jqTGrid and go one step further. – What if we want to hide a column? What about the column header text?
The first step what I did for that was to change Linq2Sql to the Entity Framework.
That is actually not a big deal and I was surprised that everything worked so straight but when I saw the page I figured out that there are new columns.
As you can see there are now -“EntityState” and “EntityKey”.Where do they come from? Ya right if you look at the HaakOverflowModel.designer.cs you can see that the entities have a base class “EntityObject” which contains the two properties.
As you can see we need to hide these columns because otherwise if we use EF we always would have these columns in our grid. My idea was to make use of the DataAnnotations where you have the “ScaffoldColumnAttribute” and the “DisplayNameAttribute”.
So I ended up making a class called “Question.cs” which extends the EF-Question class and a second class “QuestionMetaData” to associate the DataAnnotation attributes.
[MetadataType(typeof(QuestionMetaData))]
public partial class Question
{
}
public class QuestionMetaData
{
[ScaffoldColumn(true)]
[DisplayName(“My Id column“)]
public int Id { get; set;
}
First I thought I will check with reflection if there are any attributes and just hide the columns but then I remembered that Microsoft uses the DataAnnotation also for the HtmlHelper to create the form elements:
<%:Html.EditorForModel(Model) %>
So I downloaded the ASP.NET MVC source code and found following lines to check if the data gets rendered or not:
metadata.ShowForDisplay
&& metadata.ModelType != typeof(EntityState)
&& !metadata.IsComplexType
The next step was now to check our metadata model if there are such properties and based on it show the columns or not. For that I used the “GetMetadataForProperty”method:
ModelMetadata metadata =
ModelMetadataProviders.Current.GetMetadataForProperty(() => null, objType, property.Name);
if (Guard.IsValidColumn(metadata)) {
if (metadata.DisplayName != null)
displayName = metadata.DisplayName;
sb.AppendFormat(“‘{0}’,“, displayName);
}
After that I thought how we can configure the column width. So I came up with the idea to create an own “JqTGriddAttribute” – this is really easy – you just have to inherit from the “Attribute” class and give the properties you want – in our example “ColumnWidth”.
The bigger challenge was now to read the attributes because we are passing the “Question” class which is associated with the “QuestionMetaData” class and which holds the data annotiations.
But I found a great post from David Ebbo where he shows how to access access the attributes:
TypeDescriptor.AddProvider(new AssociatedMetadataTypeTypeDescriptionProvider(objType),objType);
var propertyList =
TypeDescriptor.GetProperties(objType).Cast<PropertyDescriptor>().Where(w=>w.Name==propertyName).Single();
With the usage of the “DataAnnotations” you have now the full control of your data model and the rendering of the grid.
Here is the new source code to play around.
My next idea is now to have a base controller which implements already the controller method for the grid so that you only would need to place the grid on the page! – You can find that here.
Trackbacks/Pingbacks