DataGrid AutoGenerateColumns and Id property

Sep 16, 2008 at 4:03 PM
I am programatically setting ItemSource to an ObservableCollection<MyRecord>
class MyRecord
   public int Id { get; set;  }  // force on me by the binding

I have AuotGenerateColumns= true so the DataGrid is using reflection to generate the columns.
if MyRecord does contain a public property ID I get a binding error.

System.Windows.Data Error: 39 : BindingExpression path error: 'Id' property not found on 'object' ''MyRecord' (HashCode=9832661)'. BindingExpression:Path=Id; DataItem='MyRecord' (HashCode=9832661); target element is 'DataGridRow' (Name=''); target property is 'Header' (type 'Object')

I would prefer to get rid of Id.  Is there a way to tell binding not to look for the property?
If I have to have it as a property how can I tell data grid that it is not a column. 
I don't want users to see it.


Sep 17, 2008 at 1:24 PM
In the AutoGeneratingColumn event, you can cancel the operation on the particular columns you don't want.  In the auto-generating scenario the header name will always be the property name so you can look for that and if it is "Id", then cancel the operation.