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;
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.
Ruslan
source share