简体   繁体   English

复杂的数据绑定到DataGridView标头文本

[英]Complex databinding to DataGridView header text

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. 无济于事,并且DataGridViewColumnDataGridViewColumnHeaderCell都没有DataBindings属性。

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 ; 对于更复杂的要求(仅出于完整性),如果编写自己的PropertyDescriptor实现(由TypeDescriptionProviderITypedList ,或者甚至更好)为IBindingList实现(可以在其中引发ListChanged类型的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. 然而,任何这些是工作显著量,它根本不值得只是为了你的建议是什么。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM