Use DisplayNameAttribute in ASP.NET

I want to bind a List to a GridView on a webpage, but override the way that property names are displayed via annotation. I thought System.ComponentModel would work, but this does not seem to work. Is it only for Windows Forms ?:

using System.ComponentModel; namespace MyWebApp { public class MyCustomClass { [DisplayName("My Column")] public string MyFirstProperty { get { return "value"; } } public MyCustomClass() {} } 

Then on the page:

 protected void Page_Load(object sender, EventArgs e) { IList<MyCustomClass> myCustomClasses = new List<MyCustomClass> { new MyCustomClass(), new MyCustomClass() }; TestGrid.DataSource = myCustomClasses; TestGrid.DataBind(); 

}

This displays with "MyFirstProperty" as the column heading, not "My column". Shouldn't that work?

+6
source share
5 answers

What did SirDemon say ...

The answer seems no, you cannot. At least not out of the box.

System.Web.UI.WebControls.GridView uses the name of the reflected property:

 protected virtual AutoGeneratedField CreateAutoGeneratedColumn(AutoGeneratedFieldProperties fieldProperties) { AutoGeneratedField field = new AutoGeneratedField(fieldProperties.DataField); string name = fieldProperties.Name; //the name comes from a PropertyDescriptor ((IStateManager) field).TrackViewState(); field.HeaderText = name; //<- here reflected property name field.SortExpression = name; field.ReadOnly = fieldProperties.IsReadOnly; field.DataType = fieldProperties.Type; return field; } 

While System.Windows.Forms.DataGridView uses a DisplayName if available:

 public DataGridViewColumn[] GetCollectionOfBoundDataGridViewColumns() { ... ArrayList list = new ArrayList(); //props is a collection of PropertyDescriptors for (int i = 0; i < this.props.Count; i++) { if (...) { DataGridViewColumn dataGridViewColumnFromType = GetDataGridViewColumnFromType(this.props[i].PropertyType); ... dataGridViewColumnFromType.Name = this.props[i].Name; dataGridViewColumnFromType.HeaderText = !string.IsNullOrEmpty(this.props[i].DisplayName) ? this.props[i].DisplayName : this.props[i].Name; } } DataGridViewColumn[] array = new DataGridViewColumn[list.Count]; list.CopyTo(array); return array; } 

Unfortunately, although you can override CreateAutoGeneratedColumn, neither the missing DisplayName nor property descriptors are passed, and you cannot override CreateAutoGeneratedColumns (although you could CreateColumns).

This means that you have to iterate over the reflected properties yourself and elsewhere.

+2
source share

If all you care about is the header text in the GridView, just use the HeaderText property for each field you associate. If you create auto-generating columns, you simply set the HeaderText after binding the GridView.

If you want the GridView to take into account any attribute that you put in the properties of your associated class, I believe that you need to create your own GridView.

Maybe I'm wrong, but I have not seen a single ASP.NET grid from control providers (at least Telerik, Janus Systems and Infragistics). If you do, you can sell them an idea.

+2
source share

When using .net 4 or later, you can use gridview1.enabledynamicdata (typeof (mytype)). I did not consider all the types that you can use there, but I know that [displayname ("somename")] works well, but [viewed (false)] does not skip the column from the grid. It seems to knit one of MS. at least you can easily rename the column names and omit the column, I just declare the variable instead of using the property. It has the same effect ...

By the way, using a constructor to create columns is a simple way out, but just showing a different column name takes a lot of time, especially with classes with many fields.

+2
source share

Do you use .net4, what you need to do is set the enabledynamicdata as a grid to true.

+1
source share

You can do it now on asp.net mvc2. It works that way.

0
source share

All Articles