DataGridView:格式化值而不实际更改有界数据?
我一直在网上搜索,但未能找到答案。
我有一个带有 BindingSource 边界的 DataGridView,它有一个自定义类的对象列表。在类的字段中,我有一个字符串字段,我想使用它来显示它
Path.GetFileName();
,因为它包含整个文件路径,我想要的是仅显示文件的名称,但保持文件路径(即,有界数据完整)有界物体。
有什么方法可以做到这一点(格式、模板、样式...)?因为每当我更改单元格的值字段时,它都会更改对象的值(这是逻辑)。
预先非常感谢
I've been searching through the web but I have not been able to find an answer.
I have a DataGridView with a BindingSource bounded, which has a List of objects of a custom class. Among the fields of the class, I have a string field which I want to show using
Path.GetFileName();
because it contains the whole filepath and what I want is to show just the name of the file but keeping the filepath (i.e., the bounded data intact) in the bounded object.
It is there any way to do this (Format, template, styles, ...)? because anytime I change the Value field of the Cell, it changes the value of the object (which it's logic).
Many thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用 DataGridView.CellFormatting 事件数据网格视图。这将为您提供一个 DataGridViewCellFormattingEventArgs 对象,其中包含有关当前单元格的信息,包括行和列以及未格式化的值。将 Value 属性更新为格式化值并将 FormattingApplied 设置为 true,DataGridView 将呈现该值。
像这样的事情:
Use the DataGridView.CellFormatting event on the DataGridView. This will give you a DataGridViewCellFormattingEventArgs object with information about the current cell, including the row and column and the unformatted value. You update the Value property to the formatted value and set FormattingApplied to true, and the DataGridView will render that value.
Something like this:
绑定
类有一个Format()
和 < code>Parse() 事件处理程序,您可以在其中定义如何以两种方式格式化数据。The
Binding
class has aFormat()
andParse()
event handler where you can define how the data will be formatted in both ways.