I'm using Devexpress Gridcontrol in my application which is developing at quite a rapid pace, and this often involves refactoring, changing property names etc as requirements become more clear / evolve.
Because the property names are hard-coded into a string in the form.designer.cs
file, this makes it very difficult to pick up with refactoring. I use Resharper and I'm aware you can select the box to search in string literals, but when the text is Description
or Manufacturer
this appears hundreds of times in unrelated string literals across my project so to implement this way would be time consuming and frankly a waste of time.
Using the nameof
operator provides the bridge between specifying field names explicitly, whilst also providing out of box support for refactoring.
Take this snippet from my designer file.
//
// colManufacturer
//
this.colManufacturer.Caption = "Manufacturer";
this.colManufacturer.ColumnEdit = this.xrefManufacturerSearch;
this.colManufacturer.FieldName = "Manufacturer";
this.colManufacturer.Name = "colManufacturer";
this.colManufacturer.Visible = true;
this.colManufacturer.VisibleIndex = 1;
If I replace the FieldName
attribute to
this.colManufacturer.FieldName = nameof(CrossRef.Manufacturer);
But now i cannot open the designer for this form, error code 'The designer cannot process unknown name nameof
at line 125. The code within the method InitializeComponent is generated by the designer and should not be manually modified.
Is there any way of using nameof expressions for Devexpress Field Names?
You should never do any refactoring within the *.designer.cs
files because of
The code within the method InitializeComponent is generated by the designer and should not be manually modified
The main reason for this restriction that the Code-Dom serialization infrastructure is based on the restricted subset of С#/VB syntax. Thus it does not support some specific language features e.g. nameof
. Take a look at the CSharpCodeProvider to learn which expressions are allowed or contact the Visual Studio Team for more information.
Regarding the DevExpres GridControl columns customization, I suggest you take into account the fact that GridControl supports the wide range of annotation attributes which allows you to specify how your columns will be displayed, formatted and validated without design time customization at all. You should only apply the specific annotations at the DTO level and then assign these objects collection directly to the GridControl's DataSource property:
public class CrossRef{
[Display(Name = "MANUFACTURER")]
[...]
public string Manufacturer { get; set; }
}
// ...
gridControl1.DataSource = new BindingList<CrossRef> {
new CrossRef() { Manufacturer = ... },
...
};