views:

1159

answers:

1

Is there a way to complex databind to a column's header text? Or is the only way to manually set the values and listen for change events from the source?

For the record, I've tried both

MyDataGrid.DataBindings.Add("Columns[<columnIndex>].HeaderText",MySource,"MyProperty");

and

MyDataGrid.DataBindings.Add("Columns[\"ColumnName\"].HeaderText",MySource,"MyProperty");

to no avail, and neither DataGridViewColumn and DataGridViewColumnHeaderCell have a DataBindings property.

+1  A: 

No, the column headers don't support data binding, since it is understood that this metadata will be (largely) static.

In this case, I would simply do it manually, as you propose (read from a property and subscribe to an event to update the column header).

For more complex requirements (for completeness only), you can take more control if you write your own PropertyDescriptor implementation, exposed by either TypeDescriptionProvider or ITypedList - or even better, an IBindingList implementation where you raise ListChanged events of type ListChangedType.PropertyDescriptorChanged; however any of these is a significant amount of work, and it simply isn't worth it just for what you are suggesting.

Marc Gravell