1

Im using a datagridview with autogeneratecolumns on. I can use something like this to get a handler to a particular column:

public int MyProperty { get; set;}

....

myDataGridView.Columns["MyProperty"];

Which is not optimal (it means that if I change the name of MyProperty I need to change all the "MyProperty" strings in the code). Is there a way to overcome this problem? Can I, for example, use an attribute to make the column identifier independent of the property name (without resorting to manual columns creation)?

Galactus
  • 806
  • 7
  • 10

1 Answers1

1

One easy way is to make use of an expression which can evalaute the property and essentially give you the strong typing ability so that should you re-factor your property name it will propagate.

A sample can be found here...as it is very common for this to surface when implementing INotifyPropertyChanged as the loose typing surfaces there as well.

Community
  • 1
  • 1
Aaron McIver
  • 24,527
  • 5
  • 59
  • 88